React Error Boundaries: Containing UI Crashes

An Error Boundary is like a try...catch block for React components, preventing a single UI crash from breaking your whole app. Use it to wrap components that might fail, showing a fallback UI instead of a white screen.
Why it exists
Before Error Boundaries, any JavaScript error inside a component during rendering would corrupt React's internal state and cause the entire app to unmount, showing a blank page. This provided a terrible user experience for recoverable errors. Error Boundaries were created to solve this by localizing the damage.
The mental model
An Error Boundary is a special React component that acts like an electrical circuit breaker for your component tree. When a component inside it "trips" by throwing an error during rendering, the boundary catches the error, cuts off the broken part of the tree, and renders a fallback UI in its place. The rest of the application keeps running normally.
How it works
You create an Error Boundary by defining a class component with one or both of these lifecycle methods: static getDerivedStateFromError(error) and componentDidCatch(error, errorInfo). When a child component throws an error, React calls getDerivedStateFromError first. This method should return a state object to update the boundary's state, triggering a re-render to show a fallback UI. Then, componentDidCatch is called, which is useful for side effects like logging the error. Crucially, there is no function component equivalent; you must use a class component for Error Boundaries.
When to use it
Use Error Boundaries to wrap distinct parts of your application that can fail independently. Good candidates are complex data visualizations, third-party widgets you don't control, or any feature that might encounter unexpected API responses during render. Place them at a strategic level—not around every button, but around logical sections of your app.
When not to use it
Error Boundaries do NOT catch all errors. They miss errors inside event handlers (like onClick), asynchronous code (like setTimeout or fetch calls), server-side rendering, and errors thrown in the error boundary component itself. For these, you must use standard JavaScript try...catch blocks and promise .catch() handlers.
One canonical example
To use it, you wrap a component like this: <ErrorBoundary><MyRiskyComponent /></ErrorBoundary>. The boundary itself is a class that uses state to decide whether to render its children or a fallback UI. It uses getDerivedStateFromError to update that state and componentDidCatch to log the error details, like this: class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError(error) { return { hasError: true }; } componentDidCatch(error, info) { logErrorToService(error, info.componentStack); } render() { if (this.state.hasError) { return <h1>Something went wrong.</h1>; } return this.props.children; } }
Interview question
What is the primary role of the static getDerivedStateFromError(error) lifecycle method within a React Error Boundary?
- a.To asynchronously fetch new data to recover from the error.
- b.To prevent the error from propagating to parent components by re-throwing it.
- c.To log the error details to an external error tracking service.
- d.To update the boundary's state, triggering a re-render to display a fallback UI.Correct
Why? this is the answer
The card explicitly states that getDerivedStateFromError's role is to return a state object to update the boundary's state, triggering a re-render to show a fallback UI. Logging errors is handled by the componentDidCatch method.
Just read this? Test yourself on what you have been reading.
Read the original → react.dev
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on react — each one lists the topics its interview covers.
See open roles