Wrap an ErrorBoundary around the part of the tree where you want to show a fallback UI if rendering fails. Your fallback can call resetErrorBoundary to clear the error and retry rendering:
This package is built on top of React error boundaries, so it follows React's rules for what errors are caught.
Error boundaries catch errors thrown while rendering the tree below them.
Error boundaries do not catch errors thrown during:
Server side rendering
Event handlers
Errors thrown in the error boundary itself
Async code that runs after rendering, like setTimeout callbacks or unresolved promises
Event handler and async errors
For event handler errors and async callback errors:
Use useErrorBoundary to pass caught errors to the nearest boundary.
In React 19, useTransition Actions can be an alternative: errors thrown from a function passed to the returned startTransition function are caught by the nearest boundary.
"use client";import { useTransition } from "react";import { ErrorBoundary } from "react-error-boundary";function AddCommentContainer() { return ( <ErrorBoundary fallback={<p>Could not add comment</p>}> <AddCommentButton /> </ErrorBoundary> );}function AddCommentButton() { const [isPending, startTransition] = useTransition(); function handleClick() { startTransition(async () => {
API
ErrorBoundary
A reusable React error boundary component.
Wrap this component around other React components to "catch" errors and render a fallback UI.
Catches errors thrown while rendering the tree below it.
Does not catch errors thrown during:
Server side rendering
Event handlers
Errors thrown in the error boundary itself
Async code that runs after rendering, like setTimeout callbacks or unresolved promises
Event handler and async errors:
Use useErrorBoundary to pass caught errors to the nearest boundary
In React 19, errors thrown from a function passed to the startTransition function returned by useTransition
are caught by the nearest boundary
ℹ️ The component provides several ways to render a fallback: fallback, fallbackRender, and FallbackComponent.
Refer to the documentation to determine which is best for your application.
ℹ️ This is a client component. You can only pass props to it that are serializable or use it in files that have a "use client"; directive.
Required props
None
Optional props
Name
Description
onError
Optional callback to enable e.g. logging error information to a server.
@param error Value that was thrown; typically an instance of Error
@param info React "component stack" identifying where the error was thrown
onReset
Optional callback to be notified when an error boundary is reset so React can retry the failed render.
resetKeys
When changed, these keys will reset a triggered error boundary.
This can be useful when an error condition may be tied to some specific state (that can be uniquely identified by key).
See the documentation for examples of how to use this prop.
fallback
Static content to render in place of an error if one is thrown.
<ErrorBoundary fallback={<div className="text-red">Something went wrong</div>} />
FallbackComponent
FAQ
ErrorBoundary cannot be used as a JSX component
This error can be caused by a version mismatch between react and @types/react. To fix this, ensure that both match exactly, e.g.: