https://github.com/ichengbo/react-native-error-helper
A helper for React Native to catch global JS errors and provide some ways to resolve error boundaries.
https://github.com/ichengbo/react-native-error-helper
react-native
Last synced: about 1 year ago
JSON representation
A helper for React Native to catch global JS errors and provide some ways to resolve error boundaries.
- Host: GitHub
- URL: https://github.com/ichengbo/react-native-error-helper
- Owner: iChengbo
- License: mit
- Created: 2021-04-17T13:30:09.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-02-15T20:59:36.000Z (over 3 years ago)
- Last Synced: 2025-04-24T01:09:45.818Z (about 1 year ago)
- Topics: react-native
- Language: TypeScript
- Homepage:
- Size: 108 KB
- Stars: 19
- Watchers: 2
- Forks: 3
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# react-native-error-helper
> A helper for React Native to catch global JS errors and provide some ways to resolve error boundaries.
[](./LICENSE)
[](https://www.npmjs.com/package/react-native-error-helper)
[](https://www.npmjs.com/package/react-native-error-helper)
[](https://github.com/iChengbo/react-native-error-helper)
## Table of Contents
- [Install](#Install)
- [Usage](#Usage)
- [setGlobalErrorHandler](#setGlobalErrorHandler)
- [setPromiseUnCatchHandler](#setPromiseUnCatchHandler)
- [ErrorBoundary](#ErrorBoundary)
- [withErrorBoundary](#withErrorBoundary)
- [LICENSE](#LICENSE)
## Install
> yarn add react-native-error-helper
## Usage
### setGlobalErrorHandler
```js
import { setGlobalErrorHandler } from 'react-native-error-helper';
setGlobalErrorHandler((error, isFatal) => {
console.log('global error:', error, isFatal);
}, true);
```
### setPromiseUnCatchHandler
```js
import { setPromiseUnCatchHandler } from 'react-native-error-helper';
setPromiseUnCatchHandler((id, err) => {
console.log('promise un catch:', err);
}, true);
```
### ErrorBoundary
```js
import { ErrorBoundary } from 'react-native-error-helper';
const App = () => (
)
```
### withErrorBoundary
#### class component
```js
import { withErrorBoundary } from 'react-native-error-helper';
@withErrorBoundary({
renderBoundary: ({error}) => {
return catch error: {error.message};
},
})
class BugCenter extends React.Component {
constructor(props) {
super(props);
this.state = {
isError: false,
};
}
render() {
const {isError} = this.state;
if (isError) {
throw new Error('💥');
} else {
return (
{
this.setState({
isError: true
});
}}>
{String(isError)}
);
}
}
}
```
#### function component
```js
import { withErrorBoundary } from 'react-native-error-helper';
const BugCenter = props => {
const [isError, setIsError] = useState();
if (isError) {
throw new Error('💥');
} else {
return (
{
this.setState({
isError: true
});
}}>
{String(isError)}
)
}
}
const SafeCenter = withErrorBoundary({
renderBoundary: ({error}) => {
return catch error: {error.message};
},
})(BugCenter);
```
## LICENSE
MIT