use-error-boundary
A react hook for using error boundaries in your functional components.
It lets you keep track of the error state of child components, by wrapping them in a provided ErrorBoundary
component.
Installation
npm i use-error-boundary
Examples and usage
Import the hook:
import { useErrorBoundary } from "use-error-boundary"
import useErrorBoundary from "use-error-boundary"
Use the hook in your react component,
it provides you with this object:
const MyComponent = () => {
const {
ErrorBoundary,
didCatch,
error,
errorInfo
} = useErrorBoundary()
...
}
Wrap your components in the ErrorBoundary
:
const JustRenderMe = () => {
throw new Error('💥')
}
const MyComponent = () => {
...
return (
<ErrorBoundary>
<JustRenderMe />
</ErrorBoundary>
)
}
Optionally, you can pass a render() and renderError() function to render the components to display errors in the boundary itself:
return (
<ErrorBoundary
render={() => <SomeChild />}
renderError={({ error }) => <MyErrorComponent error={error} />}
/>
)
TODO
Contributing
Contributions are welcome, as this is my first properly published npm package.
Feel free to open issues or pull requests! I will review them as fast as possible.