Skip to content

Instantly share code, notes, and snippets.

@vitorbritto
Last active May 14, 2023 18:59
Show Gist options
  • Save vitorbritto/e853cf034b01a5f19b5ba50dc0780885 to your computer and use it in GitHub Desktop.
Save vitorbritto/e853cf034b01a5f19b5ba50dc0780885 to your computer and use it in GitHub Desktop.
React Error Boundary
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