I'm using react-query and react-error-boundary library in my Next.js project. I've noticed that if I use the reset function within the QueryErrorResetBoundary component, it triggers a re-render. However, when I navigate back without using React Query's reset, and instead use resetErrorBoundary as shown in the code below, the fallback screen disappears. Can you explain why this happens?
/ /ErrorBoundaryLayout
function ErrorBoundaryLayout({children}) {
return (
<QueryErrorResetBoundary>
{({ reset }) => (
<ErrorBoundary
// onReset={reset} I added comments
fallbackRender={({ error, resetErrorBoundary }) => {
return (
<ErrorFallback
resetErrorBoundary={resetErrorBoundary}
/>
);
}}
>
{children}
</ErrorBoundary>
)}
</QueryErrorResetBoundary>
);
}
//ErrorFallback
unction ErrorFallback({resetErrorBoundary}) {
const router = useRouter();
// this doesn't work
const navigateToBack = () => {
resetErrorBoundary();
router.back();
};
// this way works
const errorLocation = useRef(router.asPath);
useEffect(() => {
if (errorLocation.current !== router.asPath) {
resetErrorBoundary();
}
}, [router.asPath]);
return (
<Button onClick={router.back}/>
);
}
resetfrom theQueryErrorResetBoundarymakes sure that the Query will re-fetch when it is mounted the next time. The flow is:resetErrorBoundary, which will unmount the boundary and show the component againresetso that it can fetch again.