Last active
July 31, 2021 18:57
-
-
Save vkiranmaniya/08586bb1fa933dfcca253d6e29db1d41 to your computer and use it in GitHub Desktop.
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 React, { Component } from 'react'; | |
export const withErrorBoundary = WrappedComponent => ( | |
class extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { error: null, errorInfo: null }; | |
} | |
componentDidCatch = (error, errorInfo) => catchFunc(error, errorInfo, this) | |
render() { | |
if (this.state.errorInfo) { | |
return handleError(this) | |
} | |
// Normally, just render children | |
return <WrappedComponent {...this.props} />; | |
} | |
} | |
); | |
const catchFunc = (error, errorInfo, ctx) => { | |
// catch errors in any components below and re-render with error message | |
ctx.setState({ | |
error: error, | |
errorInfo: errorInfo | |
}) | |
// log error messages, etc. | |
} | |
const handleError = (ctx) => ( | |
// Error path | |
<div style={ctx.props.style || styles.error}> | |
<h2>Something went wrong.</h2> | |
<details style={{ whiteSpace: 'pre-wrap' }}> | |
{ctx.state.error && ctx.state.error.toString()} | |
<br /> | |
{ctx.state.errorInfo.componentStack} | |
</details> | |
</div> | |
); | |
const styles = { | |
error: { | |
backgroundColor: '#f98e7e', | |
borderTop: '1px solid #777', | |
borderBottom: '1px solid #777', | |
padding: '12px', | |
} | |
} | |
export default withErrorBoundary; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment