Skip to content

iChengbo/react-native-error-helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

class component

import { withErrorBoundary } from 'react-native-error-helper';

@withErrorBoundary({
  renderBoundary: ({error}) => {
    return <Text>catch error: {error.message}</Text>;
  },
})
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 (
        <Text
          onPress={() => {
            this.setState({
              isError: true
            });
          }}>
          {String(isError)}
        </Text>
      );
    }
  }
}

function component

import { withErrorBoundary } from 'react-native-error-helper';

const BugCenter = props => {
  const [isError, setIsError] = useState();
  if (isError) {
    throw new Error('💥');
  } else {
    return (
      <Text
        onPress={() => {
          this.setState({
            isError: true
          });
        }}>
        {String(isError)}
      </Text>
    )
  }
}

const SafeCenter = withErrorBoundary({
  renderBoundary: ({error}) => {
    return <Text>catch error: {error.message}</Text>;
  },
})(BugCenter);

LICENSE

MIT

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published