Exempel:
import React from 'react';
<ErrorBoundary fallback={<div>Something went wrong</div>}>
<Component1 />
<Component2 />
</ErrorBoundary>
npm i react-error-boundary
import React from 'react';
const ErrorFallback = () => (
<div>
<h1>Oops! Something went wrong.</h1>
<p>Try refreshing the page.</p>
</div>
);
const ErrorBoundary = ({ children }) => (
<ErrorBoundary
FallbackComponent={ErrorFallback}
onReset={() => window.location.reload()}
>
{children}
</ErrorBoundary>
);
export default ErrorBoundary;
componentDidCatch-metoden.https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary