Last active
May 14, 2023 18:59
-
-
Save vitorbritto/e853cf034b01a5f19b5ba50dc0780885 to your computer and use it in GitHub Desktop.
React Error Boundary
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 { Component } from 'react'; | |
const ErrorComponent = () => { | |
return <h1>Something went wrong</h1>; | |
}; | |
export class AppError extends Component { | |
state = { | |
hasError: false, | |
}; | |
static getDerivedStateFromError = error => { | |
return { hasError: true }; | |
}; | |
componentDidCatch = (error, info) => { | |
this.setState({ error, info }); | |
}; | |
render() { | |
const { hasError } = this.state; | |
const { children } = this.props; | |
return hasError ? <ErrorComponent /> : children; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment