Performance: Error recovery with reset
MEDIUM IMPACT
This concept affects user interaction responsiveness and visual stability during error recovery in Next.js applications.
import { useState } from 'react'; import { ErrorBoundary } from 'react-error-boundary'; function ResettableErrorBoundary({ children }) { const [resetKey, setResetKey] = useState(0); return ( <ErrorBoundary fallbackRender={({ resetErrorBoundary }) => ( <div> <p>Something went wrong.</p> <button onClick={() => { resetErrorBoundary(); setResetKey(k => k + 1); }} aria-label="Reset error boundary"> Try again </button> </div> )} onReset={() => setResetKey(k => k + 1)} resetKeys={[resetKey]} > {children} </ErrorBoundary> ); }
function ErrorBoundary({ children }) { const [hasError, setHasError] = React.useState(false); React.useEffect(() => { window.addEventListener('error', () => setHasError(true)); }, []); if (hasError) { return <div>Error occurred. Please reload the page.</div>; } return children; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Full page reload on error | High (whole DOM reload) | Multiple reflows | High paint cost | [X] Bad |
| Error boundary reset with resetKeys | Low (subtree only) | Single reflow | Low paint cost | [OK] Good |