Reset and Recovery Mechanisms
You'll learn how to give users a way to recover from errors without reloading the entire page.
When an error boundary catches a problem, you don't want users stuck staring at an error screen forever. Reset and recovery mechanisms provide escape hatches—ways to attempt recovery and restore your application to a working state.
The Problem with Static Error UIs
After your error boundary displays a fallback UI (from lesson 1670), users need a path forward. Without a reset mechanism, they'd have to manually refresh the browser, losing any unsaved state or context.
Reset Buttons
The simplest recovery mechanism is a "Try Again" or "Reset" button in your fallback UI. When clicked, it attempts to clear the error state and re-render the failed component tree:
function ErrorFallback({ error, resetErrorBoundary }) {
return (
<div role="alert">
<h2>Something went wrong</h2>
<pre>{error.message}</pre>
<button onClick={resetErrorBoundary}>
Try Again
</button>
</div>
);
}
The resetErrorBoundary function resets the boundary's internal error state, triggering a fresh render attempt.
Automatic Retry Logic
For transient errors (like network failures), you can implement automatic retry with delays:
function resetAfterDelay(resetFn, delay = 3000) {
setTimeout(() => {
resetFn();
}, delay);
}
This is like a circuit breaker cooling down—giving the system time before attempting recovery.
When to Use Each
Manual reset: User-triggered errors, logic bugs, or when you need user acknowledgment.
Automatic retry: Network timeouts, temporary API failures, or loading errors that might resolve themselves.
Key Takeaway: Reset buttons and automatic retry logic transform dead-end error screens into recoverable moments, letting users continue without losing their session or forcing a full page reload.