react-native-error-boundary

website·Indexed 29 days ago

https://react-native-error-boundary.js.org/

A library for implementing error boundaries in React Native. It provides an ErrorBoundary component to catch JavaScript errors in child component trees, allowing developers to log errors via the onError prop, recover from errors, and render custom fallback UIs using the FallbackComponent.

Tokens
1.3K
Snippets
8
Records
13
Agent score
51%

What's inside react-native-error-boundary

  1. Prevent React Native app crashes using Error Boundaries

    In React, uncaught JavaScript errors cause the entire component tree to unmount, which typically results in the user seeing an empty white screen without feedback. Error Boundaries solve this by catching JavaScript errors within their child component tree, allowing the developer to provide a fallback UI instead of a complete app crash. Note that Error Boundaries only handle JavaScript errors; native errors are not handled.
  2. Prevent React component tree unmounting with Error Boundaries

    Since React 16, any error that is not caught by an error boundary will cause the entire React component tree to unmount, resulting in a complete application crash for the user.
  3. Implement basic error handling with ErrorBoundary

    Wrap components that may throw errors with the ErrorBoundary component. When a child component throws an error, the ErrorBoundary catches it and renders a fallback screen to notify the user, preventing the entire application from crashing.
    import ErrorBoundary from 'react-native-error-boundary'
    
    const App = () => (
      <ErrorBoundary>
        <ChildrenThatCouldThrowEror />
      </ErrorBoundary>
    )
  4. Install react-native-error-boundary

    Install the react-native-error-boundary package using npm, yarn, or pnpm to add error boundary capabilities to your React Native project.
    npm install react-native-error-boundary
  5. Render a custom fallback UI using ErrorBoundary

    To customize the screen displayed to the user when an error occurs, use the FallbackComponent property of the ErrorBoundary component. The FallbackComponent receives two props: error (the Error object) and resetError (a function to clear the error state and attempt a re-render).
    import ErrorBoundary from 'react-native-error-boundary'
    import { View, Text, Button } from 'react-native'
    
    const CustomFallback = (props: { error: Error, resetError: Function }) => (
      <View>
        <Text>Something happened!</Text>
        <Text>{props.error.toString()}</Text>
        <Button onPress={props.resetError} title={'Try again'} />
      </View>
    )
    
    const App = () => (
      <ErrorBoundary FallbackComponent={CustomFallback}>
        <ChildrenThatCouldThrowEror />
      </ErrorBoundary>
    )
    
  6. Install react-native-error-boundary

    Install the react-native-error-boundary package using your preferred package manager (npm, yarn, or pnpm) to add error boundary capabilities to your React Native project.
    npm install react-native-error-boundary
    # or
    yarn add react-native-error-boundary
    # or
    pnpm add react-native-error-boundary
  7. Log errors using the ErrorBoundary onError prop

    To log errors to an external monitoring service (such as Bugsnag or Sentry), provide an onError callback function to the ErrorBoundary component. The onError function receives the Error object and the stackTrace as arguments, allowing you to capture and report crashes occurring within the wrapped component tree.
    import ErrorBoundary from 'react-native-error-boundary'
    
    const errorHandler = (error: Error, stackTrace: string) => {
      /* Log the error to an error reporting service like Sentry or Bugsnag */
      console.log('Error caught:', error);
      console.log('Stack trace:', stackTrace);
    }
    
    const App = () => (
      <ErrorBoundary onError={errorHandler}>
        <ChildrenThatCouldThrowEror />
      </ErrorBoundary>
    )
  8. Compare react-native-error-boundary and react-error-boundary

    The primary difference between react-native-error-boundary and react-error-boundary is the target platform. react-native-error-boundary is specifically designed for React Native and includes a default FallbackComponent built with React Native components, making it incompatible with React for the web. While react-error-boundary is more feature-rich and generally compatible with React Native, react-native-error-boundary provides a more tailored experience for mobile development.
  9. Handle errors using the ErrorBoundary onError prop

    The onError prop allows you to execute custom logic when an error is caught by the ErrorBoundary. The function receives two arguments: the error (of type Error) and the stackTrace (of type string).
    onError(error: Error, stackTrace: string): void
  10. Configure the ErrorBoundary component props

    The ErrorBoundary component accepts the following props to manage error catching and fallback UI:

    • Children (Required): The React children components that may throw an error.
    • FallbackComponent (Optional): A custom React component to render when an error is caught. If not provided, the library uses a built-in default fallback component.
    • onError (Optional): A callback function triggered when an error is caught. It provides the error object and the stack trace for logging or reporting purposes.
  11. Handle red error screens during React Native development

    In development mode, React Native displays a red error screen (LogBox) to provide detailed debugging information. This screen overlays the ErrorBoundary component. To see the actual ErrorBoundary fallback UI during development, dismiss the red screen or press the Esc key.