Skip to content

Instantly share code, notes, and snippets.

@notgiorgi
Last active August 1, 2017 07:38
Show Gist options
  • Save notgiorgi/38a46d9f338b55c572e7e0c0ce48e18e to your computer and use it in GitHub Desktop.
Save notgiorgi/38a46d9f338b55c572e7e0c0ce48e18e to your computer and use it in GitHub Desktop.
import React from 'react'
import curry from 'lodash.curry'
function withErrorHandler(errorCallback, FallbackComponent, Component) {
return class extends React.Component {
constructor() {
super()
this.state = {
hasError: false,
error: null,
errorInfo: null,
}
}
componentDidCatch(error, info) {
this.setState({ hasError: true, error, errorInfo: info })
errorCallback(error, info, this.props)
}
render() {
if (this.state.hasError) {
const { error, errorInfo } = this.state
return (
<FallbackComponent
{...this.props}
error={error}
errorInfo={errorInfo}
/>
)
}
return <Component {...this.props} />
}
static displayName = `WithErrorHandler(${Component.displayName})`
}
}
export default curry(withErrorHandler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment