Performance: Error recovery with reset
This concept affects user interaction responsiveness and visual stability during error recovery in Next.js applications.
Jump into concepts and practice - no test required
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 |
useState in a Next.js error boundary?function ErrorBoundary() {
const [error, setError] = React.useState(null);
if (error) {
return (
<div>
<p>Error occurred</p>
<button onClick={() => setError(null)}>Reset</button>
</div>
);
}
return <p>App is running</p>;
}function ErrorBoundary() {
const [error, setError] = React.useState(null);
try {
if (error) throw new Error('Error!');
return <p>App running</p>;
} catch {
return <button onClick={() => setError(false)}>Reset</button>;
}
}