Last active
August 1, 2017 07:38
-
-
Save notgiorgi/38a46d9f338b55c572e7e0c0ce48e18e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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