Skip to content

Instantly share code, notes, and snippets.

@egeste
Created November 30, 2018 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egeste/c873746f83bedc64f34bf6560abfbe2c to your computer and use it in GitHub Desktop.
Save egeste/c873746f83bedc64f34bf6560abfbe2c to your computer and use it in GitHub Desktop.
Using React 16 error boundaries in a decorator
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