Skip to content

Instantly share code, notes, and snippets.

@gurre
Created December 2, 2017 21:53
Show Gist options
  • Save gurre/39925f3e35b567a961a318b908e86c05 to your computer and use it in GitHub Desktop.
Save gurre/39925f3e35b567a961a318b908e86c05 to your computer and use it in GitHub Desktop.
How to create an error fallback using componentDidCatch in recompose
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