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

resetKeys for Automatic Recovery

Automatically resetting boundaries when specific dependencies change, useful for navigation or user context changes.

resetKeys for Automatic Recovery

You'll learn how to make error boundaries automatically reset themselves when certain data changes, eliminating manual reset button clicks.

The Problem with Manual Resets

You've seen how resetErrorBoundary() lets users manually recover from errors. But what if the underlying problem that caused the error has already been fixed by external changes? For example, if a user navigates to a different page, or logs in with valid credentials, or switches to a different data set — the error boundary should automatically clear itself without user intervention.

How resetKeys Works

The react-error-boundary library's resetKeys prop accepts an array of values to watch. Whenever any value in that array changes, the error boundary automatically resets itself — exactly as if someone had clicked a reset button.

<ErrorBoundary
  FallbackComponent={ErrorFallback}
  resetKeys={[userId, currentRoute]}
>
  <UserProfile userId={userId} />
</ErrorBoundary>

If userId or currentRoute changes, the boundary resets and re-renders its children with the fresh data.

Common Use Cases

Navigation changes: Reset when the URL path changes so errors don't persist across pages.

User context switches: Clear errors when a different user logs in or when switching between accounts.

Data dependencies: Reset when critical data IDs change, like viewing a different product or document.

Think of resetKeys like automatic form clearing — when you navigate away and come back, you expect a fresh start, not stale error states from a previous attempt.

Key Takeaway: Use resetKeys={[...dependencies]} to automatically reset error boundaries when critical context changes, providing seamless recovery without manual user action — especially powerful for navigation and authentication flows.