Git Product home page Git Product logo

react-native-error-boundary's Introduction

react-native-error-boundary

Build Status Codecov Npm Version Npm Downloads

A simple and reusable React-Native error boundary component ๐Ÿ›

Install

yarn add react-native-error-boundary

Usage

This component catches JavaScript errors anywhere in their child component tree. Native errors are not handled.

Using this component is really simple. First you have to import the ErrorBoundary component. Then, you have to wrap it around any component that could throw an error.

Basic

import ErrorBoundary from 'react-native-error-boundary'

const App = () => (
  <ErrorBoundary>
    <ChildrenThatCouldThrowEror />
  </ErrorBoundary>
)

Logging errors

You can log the error by providing an onError function to the component.

import ErrorBoundary from 'react-native-error-boundary'

const errorHandler = (error: Error, stackTrace: string) => {
  /* Log the error to an error reporting service */
}

const App = () => (
  <ErrorBoundary onError={errorHandler}>
    <ChildrenThatCouldThrowEror />
  </ErrorBoundary>
)

Custom fallback component

You can customize the appearance of the fallback component by providing the FallbackComponent prop.

import ErrorBoundary from 'react-native-error-boundary'

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></ChildrenThatCouldThrowEror>
  </ErrorBoundary>
)

API

ErrorBoundary

These are the props that the ErrorBoundary component accepts:

Property Type Required Default Description
children React.Children true Components that may throw an error
FallbackComponent React.Component false FallbackComponent UI rendered when there's an error
onError Function false Function for logging the error

FallbackComponent

These are the props that the FallbackComponent receives:

Property Type Default Description
error Error The thrown error
resetError Function A function to reset the error state

Examples

Demo

react-native-error-boundary

FAQ

react-native-error-boundary's People

Contributors

dependabot[bot] avatar carloscuesta avatar dependabot-preview[bot] avatar myckhel avatar flexbox avatar ianbayne avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.