|
1 | 1 | # react-native-error-boundary |
| 2 | + |
| 3 | +[](https://travis-ci.com/carloscuesta/react-native-error-boundary) |
| 4 | +[](https://codecov.io/gh/carloscuesta/react-native-error-boundary) |
| 5 | +[](https://codeclimate.com/github/carloscuesta/react-native-error-boundary) |
| 6 | +[](https://www.npmjs.com/package/react-native-error-boundary) |
| 7 | +[](https://www.npmjs.com/package/react-native-error-boundary) |
| 8 | + |
| 9 | +> A simple and reusable React-Native error boundary component 🐛 |
| 10 | +
|
| 11 | +## Install |
| 12 | + |
| 13 | +```bash |
| 14 | +$ yarn add react-native-error-boundary |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +Using this component is really simple. First you have to import the `ErrorBoundary` |
| 20 | +component. Then, you have to **wrap** it **around any component** that |
| 21 | +**could throw** an **error**. |
| 22 | + |
| 23 | +### Basic |
| 24 | + |
| 25 | +```jsx |
| 26 | +import ErrorBoundary from 'react-native-error-boundary' |
| 27 | + |
| 28 | +const App = () => ( |
| 29 | + <ErrorBoundary> |
| 30 | + <ChildrenThatCouldThrowEror /> |
| 31 | + </ErrorBoundary> |
| 32 | +) |
| 33 | +``` |
| 34 | + |
| 35 | +### Logging errors |
| 36 | + |
| 37 | +You can **log the error** by providing an `onError` function to the component. |
| 38 | + |
| 39 | +```jsx |
| 40 | +import ErrorBoundary from 'react-native-error-boundary' |
| 41 | + |
| 42 | +const errorHandler = (error: Error, stackTrace: string) => { |
| 43 | + /* Log the error to an error reporting service */ |
| 44 | +} |
| 45 | + |
| 46 | +const App = () => ( |
| 47 | + <ErrorBoundary onError={errorHandler}> |
| 48 | + <ChildrenThatCouldThrowEror /> |
| 49 | + </ErrorBoundary> |
| 50 | +) |
| 51 | +``` |
| 52 | + |
| 53 | +### Custom fallback component |
| 54 | + |
| 55 | +You can customize the appearance of the fallback component by providing the `FallbackComponent` prop. |
| 56 | + |
| 57 | +```jsx |
| 58 | +import ErrorBoundary from 'react-native-error-boundary' |
| 59 | + |
| 60 | +const CustomFallback = (props: { error: Error, resetError: Function }) => ( |
| 61 | + <View> |
| 62 | + <Text>Something happened!</Text> |
| 63 | + <Text>{props.error.toString()}</Text> |
| 64 | + <Button onPress={props.resetError} title={'Try again'} /> |
| 65 | + </View> |
| 66 | +) |
| 67 | + |
| 68 | +const App = () => ( |
| 69 | + <ErrorBoundary FallbackComponent={CustomFallback}> |
| 70 | + <ChildrenThatCouldThrowEror></ChildrenThatCouldThrowEror> |
| 71 | + </ErrorBoundary> |
| 72 | +) |
| 73 | +``` |
| 74 | + |
| 75 | +## API |
| 76 | + |
| 77 | +### `ErrorBoundary` |
| 78 | + |
| 79 | +These are the props that the `ErrorBoundary` component accepts: |
| 80 | + |
| 81 | +| Property | Type | Required | Default | Description | |
| 82 | +|-------------------|-------------------|----------|---------------------|------------------------------------| |
| 83 | +| children | `React.Children` | `true` | | Components that may throw an error | |
| 84 | +| FallbackComponent | `React.Component` | `false` | `FallbackComponent` | UI rendered when there's an error | |
| 85 | +| onError | `Function` | `false` | | Function for logging the error | |
| 86 | + |
| 87 | +### `FallbackComponent` |
| 88 | + |
| 89 | +These are the props that the `FallbackComponent` **receives**: |
| 90 | + |
| 91 | +| Property | Type | Default | Description | |
| 92 | +|------------|------------|---------|-------------------------------------| |
| 93 | +| error | `Error` | | The thrown error | |
| 94 | +| resetError | `Function` | | A function to reset the error state | |
| 95 | + |
| 96 | +## Demo |
| 97 | + |
| 98 | +<img |
| 99 | + src='https://user-images.githubusercontent.com/7629661/52532748-d8758400-2d29-11e9-80a0-15182517271c.gif' |
| 100 | + alt='react-native-error-boundary' |
| 101 | + width='358px' |
| 102 | +/> |
0 commit comments