react-native-error-helper
    TypeScript icon, indicating that this package has built-in type declarations

    0.2.2 • Public • Published

    react-native-error-helper

    A helper for React Native to catch global JS errors and provide some ways to resolve error boundaries.

    LICENSE npm-version npm

    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

    Install

    npm i react-native-error-helper

    DownloadsWeekly Downloads

    51

    Version

    0.2.2

    License

    MIT

    Unpacked Size

    14.3 kB

    Total Files

    13

    Last publish

    Collaborators

    • chengbo