I am trying to learn how to use Error Boundaries in React js.As i read here it prevent your application from getting crashed. But my application crash!here is my code:
App component:
function App() {
return (
<>
<h1>app component</h1>
<ErrorBoundary >
<Simple />
</ErrorBoundary>
</>
);
}
Simple Component:
export default () => {
throw new Error('I crashed!')
return <h3> simple component</h3>
}
and this my Boundaries=
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error) {
// Update state so the next render will show the fallback UI.
console.log("I got you ")
return { hasError: true };
}
componentDidCatch(error, errorInfo) {
// You can also log the error to an error reporting service
console.log(error, errorInfo);
}
render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return <h1>Something went wrong.</h1>;
}
return this.props.children;
}
}
export default ErrorBoundary;
for a very short time I see the message "Something went wrong" but after that the page crashed!
thanks for your help and advice