An open API service indexing awesome lists of open source software.

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.

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://img.shields.io/badge/license-MIT-blue)](./LICENSE)
[![npm-version](https://img.shields.io/npm/v/react-native-error-helper)](https://www.npmjs.com/package/react-native-error-helper)
[![npm](https://img.shields.io/npm/dm/react-native-error-helper.svg)](https://www.npmjs.com/package/react-native-error-helper)
[![](https://img.shields.io/github/release-date/iChengbo/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