react-native-error-helper
A helper for React Native to catch global JS errors and provide some ways to resolve error boundaries.
Table of Contents
Install
yarn add react-native-error-helper
Usage
setGlobalErrorHandler
import { setGlobalErrorHandler } from 'react-native-error-helper';
setGlobalErrorHandler((error, isFatal) => {
console.log('global error:', error, isFatal);
}, true);
setPromiseUnCatchHandler
import { setPromiseUnCatchHandler } from 'react-native-error-helper';
setPromiseUnCatchHandler((id, err) => {
console.log('promise un catch:', err);
}, true);
ErrorBoundary
import { ErrorBoundary } from 'react-native-error-helper';
const App = () => (
<ErrorBoundary>
<BugComponent />
</ErrorBoundary>
)
withErrorBoundary
import { withErrorBoundary } from 'react-native-error-helper';
@withErrorBoundary({
renderBoundary: ({error}) => {
return <Text>catch error: {error.message}</Text>;
},
})
class BuggyCounter extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0 || this.props.init,
};
}
render() {
const {count} = this.state;
if (count === 5) {
throw new Error('I error');
} else {
return (
<View>
<Text
onPress={() => {
this.setState({
count: count + 1,
});
}}>
{String(count)}
</Text>
</View>
);
}
}
}
LICENSE
MIT