Add a retry button to an error boundary
A React error boundary stays on its fallback until you reset it. Add a reset method that clears the error state and re-renders the children.
A widget throws while rendering, your error boundary swaps in a fallback, and the fallback stays on screen after the cause is gone. The user has to reload the whole page to get the widget back. A retry button fixes that, and the boundary needs one method to support it.
Add reset to the boundary
An error boundary has to be a class component, because React has no hook for it. Its static getDerivedStateFromError stores the caught error in state, and a separate flag marks the failure. JavaScript can throw falsy values, so test the flag when choosing what to render. reset clears the state, which makes React render the children again.
import { Component } from 'react'
export class ErrorBoundary extends Component {
state = { hasError: false, error: null }
static getDerivedStateFromError(error) {
return { hasError: true, error }
}
reset = () => this.setState({ hasError: false, error: null })
render() {
if (this.state.hasError) {
return this.props.fallback({ error: this.state.error, reset: this.reset })
}
return this.props.children
}
}The fallback prop is a function that receives the error and reset, so the button lives wherever you design the fallback.
Prove it with one test
The test renders a widget that throws while a flag is set. It checks the fallback, clears the flag, clicks the button and expects the widget.
// @vitest-environment jsdom
import React from 'react'
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
import { afterEach, expect, it } from 'vitest'
import { ErrorBoundary } from './ErrorBoundary.jsx'
afterEach(cleanup)
let broken = true
function Widget() {
if (broken) throw new Error('widget failed')
return <p>Widget ready</p>
}
it('shows the fallback, then renders the child again after retry', () => {
broken = true
render(
<ErrorBoundary
fallback={({ error, reset }) => (
<div role="alert">
<p>{error instanceof Error ? error.message : String(error)}</p>
<button onClick={reset}>Try again</button>
</div>
)}
>
<Widget />
</ErrorBoundary>,
{ onCaughtError: () => {} },
)
expect(screen.getByRole('alert').textContent).toContain('widget failed')
broken = false
fireEvent.click(screen.getByRole('button', { name: 'Try again' }))
expect(screen.getByText('Widget ready')).toBeTruthy()
expect(screen.queryByRole('alert')).toBeNull()
})On React 19 with a recent React Testing Library, onCaughtError silences the expected error log for this render. Running vitest run --reporter=verbose reports one passing test.
What the retry does when the cause remains
reset does not check whether the problem is fixed. It renders the children once more, and if Widget still throws, the boundary catches it again and shows the fallback. I ran a second test for that case, with broken left on, and the "Try again" button is still on screen after the click.
If the failure came from stale data, refetch it before clearing the error. A repeatable render bug will still need a code fix.
The react-error-boundary package covers the same job with a resetErrorBoundary function and a resetKeys prop. I write the class once for a small app and reach for the package when I want resetKeys.