Error handler HOC for React 16
Error handler HOC gives you an ability to wrap your components with error handling logic which is most of the times repetitive
Installation
# using npm $ npm install --save error-handler-hoc # using yarn $ yarn add error-handler-hoc
Usage
// ErrorReporter.js const ErrorReporter = // MyComponent.js Component /* ... */ { /* ... */ } const MyFallbackComponent = <div> <p>Something went wrong</p> error </div> MyFallbackComponent MyComponent
API
withErrorHandler
HOC takes 3 arguments, but it is also curried, so you can use it like this:
Fallback MyComponent// orFallbackMyComponent
and etc. See more about currying in lodash docs here
errorCallback
errorCallback
function is the first argument of the HOC and it gets called when the exception happens in render or lifecycle methods. It gets error
, errorInfo
and props that were passed to component as arguments.
FallbackComponent
FallbackComponent is the second argument of the HOC and is rendered if the exception happens. Recieves same three arguments as props as errorCallback
(error
, errorInfo
and props
)
Component
Which component to render.