Skip to content

Commit

Permalink
Reset error boundary on url change
Browse files Browse the repository at this point in the history
  • Loading branch information
ghsteff committed Nov 2, 2023
1 parent 883c48e commit e9ca055
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/shared/utils/ErrorBoundary/ErrorComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import { FC } from 'react';
import { FC, useEffect, useRef } from 'react';
import { FallbackProps } from 'react-error-boundary';
import { useLocation } from 'react-router-dom';

export const ErrorComponent: FC<{ error: any }> = ({ error }) => (
<div>
<h1>Error</h1>
<h2>Something bad happened</h2>
<pre>{error.message}</pre>
</div>
);
export const ErrorComponent: FC<FallbackProps> = ({
error,
resetErrorBoundary
}) => {
const location = useLocation();
const initialPathname = useRef(location.pathname);

useEffect(() => {
if (location.pathname !== initialPathname.current) {
resetErrorBoundary();
}
}, [initialPathname, location.pathname, resetErrorBoundary]);

return (
<div>
<h1>Error</h1>
<h2>Something bad happened</h2>
<pre>{error.message}</pre>
</div>
);
};

0 comments on commit e9ca055

Please sign in to comment.