Key takeaway
- Error handling in React uses error boundaries and try/catch. See what boundaries catch, what they miss, and how React 16 changed uncaught errors.
Error handling in React is the set of techniques that stop a JavaScript error in one part of the user interface from breaking the whole application, and its main tool is the error boundary: a class component that catches errors thrown while its child components render and shows a fallback interface instead. Errors that boundaries do not catch, such as those in event handlers, are handled with ordinary try/catch.
This guide is for React developers and engineering leads who want a clear picture of what error boundaries do, where they stop, and how they fit with testing and monitoring. It draws on the React reference for Component and the React team's post Error Handling in React 16 (26 July 2017).
What is error handling in React?
Error handling is anticipating, detecting and recovering from faults so that the normal flow of an application continues. Logic errors are found by debugging and review, compile-time errors by the toolchain, and runtime errors by code that responds when something throws. In React the runtime problem is specific: an error during rendering can take the interface down with it unless something catches it. If your team builds reactive interfaces, see our note on reactive programming solutions.
What are error boundaries and how do they work?
According to the React reference, a component becomes an error boundary when it defines static getDerivedStateFromError, componentDidCatch, or both. React calls static getDerivedStateFromError when a child component, including a distant one, throws during rendering, and the method returns the state that tells the component to display an error message. React calls componentDidCatch(error, info) so you can log the error to an error reporting service, and info.componentStack carries a stack trace that names the components involved.
The reference gives a small pattern: keep a hasError flag in state, set it in getDerivedStateFromError, log in componentDidCatch, and render a fallback when the flag is set. You declare the boundary once and reuse it across the app, wrapping the parts of the tree that can fail independently. If a boundary itself fails to render its fallback, the error moves up to the nearest boundary above it, much as a JavaScript catch block behaves.
What errors do error boundaries not catch?
The React reference lists the cases that error boundaries do not cover.
| Error source | Caught by an error boundary? | What to use |
|---|---|---|
| Rendering and lifecycle errors in child components | Yes | Error boundary with a fallback UI |
| Event handlers | No | try/catch inside the handler |
| Asynchronous code such as setTimeout or requestAnimationFrame callbacks | No (errors thrown inside a startTransition function are caught) | try/catch or promise error handling, plus reporting |
| Server side rendering | No | Server-side error handling |
| Errors thrown by the boundary itself | No | A boundary higher in the tree |
What happens to uncaught errors in React 16 and later?
The React team explained in 2017 that, as of React 16, an error that no boundary catches unmounts the entire component tree. Their reasoning was that "it is worse to leave corrupted UI in place than to completely remove it", and they gave a messaging app sending a message to the wrong person and a payments app showing a wrong amount as examples. In React 15 and earlier, errors in components could corrupt React's internal state and cause cryptic errors on later renders.
The practical lesson is to place boundaries deliberately. A boundary around the whole app gives one generic fallback, while boundaries around widgets, routes or panels let the rest of the page keep working. Pair them with an error reporting service so that production failures are seen and fixed. For the testing side, see ReactJS testing of components and unit testing in React with Jest and Enzyme.
Should you use try/catch or error boundaries?
Use both, for different jobs. The React team's post says try/catch "only works for imperative code", while React components are declarative, and an error boundary fits that model. A boundary also catches errors from deeper in the tree, for example one thrown in componentDidUpdate after a nested setState. Try/catch remains the right tool inside event handlers and asynchronous callbacks, where boundaries do not reach.
How do component stack traces help?
In development, React 16 logs errors that occur during rendering to the console, even if your application handles them, and adds a component stack trace to the message so you can see where in the component tree the failure occurred. The 2017 post notes that Create React App includes filenames and line numbers by default. In production, the React reference says component names in info.componentStack will be minified, so plan for source maps in your reporting setup. For wider performance work, see optimising React application performance.
What has changed since React 16?
Three points have moved on. First, the 2017 post says the React 15 method unstable_handleError "no longer works" and must be renamed to componentDidCatch. Second, the React reference now says that calling setState inside componentDidCatch is deprecated in favour of static getDerivedStateFromError, and it adds that there is currently no way to write an error boundary as a function component. Third, the reference points to the react-error-boundary package for teams that prefer not to write the class themselves. Class components therefore remain necessary for boundaries, even in codebases that otherwise use function components; our guide to functional vs class components covers that choice.
How do AI agents change error handling in React?
AI agents can help on the operations side of this topic. An agent that reads reported errors with their component stacks can group repeated failures, find the release that introduced them and draft a fix or a test for a developer to review. It cannot replace the boundary that protects users at runtime, so the design above stays the same. For teams building and testing agents and automation workflows, ElixirClaw is XenonStack's AI agents computing platform. Teams that want help building such applications can also see our React.JS application development services.
What should you do first?
- Write one reusable error boundary class with a clear fallback, or adopt the
react-error-boundarypackage. - Wrap independent parts of the interface, such as routes and widgets, so one failure does not clear the page.
- Log from
componentDidCatchto an error reporting service, with the component stack. - Add try/catch to event handlers and asynchronous code, which boundaries do not cover.
- Test the fallback by forcing a render error in a component test.
Next Steps in Implementing Error Handling with React
Talk to our experts about implementing error handling in React 16. Learn how industries and different teams use error boundaries and try-catch blocks to improve application stability. Utilize React error-handling strategies to enhance efficiency and responsiveness.
To map one decision your agents make, and the data and policy it depends on, book a Context Workshop. See what happens in the workshop.
Frequently asked questions
What is an error boundary in React?
An error boundary is a class component that defines static getDerivedStateFromError, componentDidCatch or both. It catches errors thrown while its child components render and shows a fallback interface.
What errors do error boundaries not catch?
According to the React reference, error boundaries do not catch errors in event handlers, asynchronous code such as setTimeout callbacks, server side rendering, or the error boundary itself.
Can an error boundary be a function component?
No. The React reference says there is currently no way to write an error boundary as a function component, although the react-error-boundary package provides one so you do not write the class yourself.
What happens to an uncaught error in React 16 and later?
An error that no error boundary catches unmounts the whole component tree. The React team chose this because leaving a corrupted interface in place is worse than removing it.
Should I use try/catch or error boundaries in React?
Use both. Error boundaries handle errors during rendering, while try/catch handles imperative code such as event handlers and asynchronous callbacks.
Follow XenonStack on LinkedIn: XenonStack on LinkedIn for new guides on agentic AI, data and platform engineering.
Ready to put AI agents to work? Explore ElixirClaw, the agentic OS platform to build, orchestrate and govern AI agents.
Explore the XenonStack platform family: ElixirData · ElixirClaw · ElixirHub · NexaStack · MetaSecure · Neural AI · AgentGRC · AgentSRE · AgentAnalyst · AgentInstruct · AgentRAI · AgentScrum · AgentSearch · AgentForce · AI Aviator · SAIF Aviator · CohortIQ · XenonStack AI · XenonStack UAE