Course contentsShow
← React
Lesson 1318 of 2,39633. Error BoundariesFree lesson

Retry and Reset Mechanisms

Implement resetErrorBoundary patterns that let users attempt recovery without full page reload.

Retry and Reset Mechanisms

You'll learn how to give users a way to recover from errors without reloading the entire page.

The Recovery Problem

When an error boundary catches a render error, the component tree is stuck in its error state. Without intervention, users would need to refresh the entire page to try again—losing any app state, form data, or navigation history. That's a terrible user experience.

The solution? Build a reset mechanism that lets the error boundary clear its error state and re-render the failed component tree, giving it another chance to succeed.

How Reset Works

Think of it like a circuit breaker that you can manually flip back on. The error boundary holds error in state. To reset, you simply clear that state:

class ErrorBoundary extends React.Component {
  state = { error: null };

  static getDerivedStateFromError(error) {
    return { error };
  }

  resetErrorBoundary = () => {
    this.setState({ error: null }); // Clear error, re-render children
  };

  render() {
    if (this.state.error) {
      return (
        <div>
          <h2>Something went wrong</h2>
          <button onClick={this.resetErrorBoundary}>
            Try Again
          </button>
        </div>
      );
    }
    return this.props.children;
  }
}

When the user clicks "Try Again," the boundary clears its error state. React re-renders the children. If the underlying problem was transient (network blip, race condition), it might succeed this time.

Making Resets Useful

A retry button alone isn't enough if the same error will happen again. Combine resets with state changes (clearing bad data, refetching resources) or pass a resetKeys prop that forces a fresh start when changed.

Key Takeaway: Reset mechanisms let users recover from errors gracefully by clearing the boundary's error state and re-rendering the failed component tree, avoiding full page reloads.