Created
December 2, 2017 21:53
-
-
Save gurre/39925f3e35b567a961a318b908e86c05 to your computer and use it in GitHub Desktop.
How to create an error fallback using componentDidCatch in recompose
This file contains hidden or 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 { omit } from 'ramda'; | |
import { | |
compose, | |
withState, | |
branch, | |
lifecycle, | |
mapProps, | |
renderComponent, | |
setDisplayName | |
} from 'recompose'; | |
import ErrorFallback from '../components/ErrorFallback'; | |
export default compose( | |
setDisplayName('ErrorBoundary'), | |
withState('hasError', 'setHasError', false), | |
lifecycle({ | |
componentDidCatch(error, errorInfo) { | |
this.props.setHasError(true); | |
console.error(error, errorInfo); | |
} | |
}), | |
branch(props => props.hasError, renderComponent(ErrorFallback)), | |
compose(mapProps, omit)(['hasError', 'setHasError']) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment