Error Boundaries
Catch errors thrown while rendering and show a friendly fallback instead of a blank white screen.
What you will learn
- Explain what an error boundary catches
- Wrap part of the UI in a boundary with a fallback
- Know what boundaries do not catch
The blank-white-screen problem
If a component throws an error while React is drawing it (say it reads user.name but user is null), React unmounts the whole app and the user sees a blank white screen. An error boundary is a special component that wraps part of your UI, catches such errors, and shows a fallback (a friendly “something went wrong” message) instead — the rest of the page keeps working.
Think of it like a circuit breaker in your house: when one appliance shorts out, the breaker trips for that circuit only, so the whole house does not go dark.
The easy way: react-error-boundary
You can write a boundary by hand (it must be a class component — the one place React still needs one), but almost everyone uses the tiny, well-known library react-error-boundary. Install it, then wrap any risky part of your app. This runs in a real project, so it is shown as code with its Output.
npm install react-error-boundaryimport { ErrorBoundary } from 'react-error-boundary';
function Fallback({ error }) {
return (
<div role="alert" style={{ padding: 16, color: '#b91c1c' }}>
<p>Something went wrong.</p>
<pre>{error.message}</pre>
</div>
);
}
function App() {
return (
<ErrorBoundary FallbackComponent={Fallback}>
<Profile /> {/* if Profile throws while rendering, Fallback shows */}
</ErrorBoundary>
);
}Here is exactly what happens:
- We write a
Fallbackcomponent — the friendly UI to show when something breaks. It receives theerrorthat was thrown and printserror.message. - We wrap the risky part —
<Profile />— in<ErrorBoundary FallbackComponent={Fallback}>. TheFallbackComponentprop tells the boundary what to show on failure. - Normally you just see
Profile. But the momentProfilethrows an error while rendering, the boundary catches it and swaps inFallbackinstead — no blank screen, and the rest ofApparound the boundary is untouched.
Note: Output:
When Profile renders normally, you see the profile. If Profile crashes (e.g. it reads a property of null), that area is replaced by a red box reading Something went wrong. with the error message underneath — the page does not go blank.
What error boundaries do NOT catch
Boundaries only catch errors thrown during rendering. They do not catch:
- Errors inside event handlers (e.g. an
onClick) — handle those with a normaltry/catch. - Errors inside async code like a
fetch.thenorsetTimeout— catch and store them in state. - Errors during server-side rendering (not relevant for a plain Vite app).
Tip: Place boundaries strategically: one near the top for a whole-app fallback, plus smaller ones around independent sections (a sidebar, a widget) so one broken widget does not take down the page.
Watch out: During development React still shows its red error overlay even with a boundary in place — that is expected. The boundary’s fallback is what your users see in the production build.
Q. What does an error boundary catch?
✍️ Practice
- Install
react-error-boundaryand wrap a component that deliberately throws, with a fallback that shows the message. - Add a “Try again” button to your fallback using the library’s
resetErrorBoundaryprop (check the docs).
🏠 Homework
- Add a top-level error boundary to one of your earlier React apps and write two sentences on what the user now sees if a component crashes.