Created
November 30, 2018 21:30
-
-
Save egeste/c873746f83bedc64f34bf6560abfbe2c to your computer and use it in GitHub Desktop.
Using React 16 error boundaries in a decorator
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, { PureComponent } from 'react' | |
import DefaultFallbackComponent from 'wherever' | |
export default (FallbackComponent = DefaultFallbackComponent) => { | |
return ComposedComponent => { | |
class ErrorBoundary extends PureComponent { | |
state: { | |
info: undefined, | |
error: undefined | |
} | |
componentDidCatch = (error, info) => { | |
this.setState({ error, info }) | |
} | |
render() { | |
const { error, info } = this.state | |
if (error) return (<FallbackComponent props={ this.props } errorBoundary={ this.state } />) | |
return (<ComposedComponent { ...this.props } />) | |
} | |
} | |
return ErrorBoundary | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment