Sub topic 1
What happens when a component throws while rendering
Prerequisites
This chapter assumes you know the following:
- React function components and JSX.
- Props, state, and the basic component lifecycle.
- JavaScript
throwandtry/catch.
You do not need to know anything about error boundaries yet. This chapter explains the problem they solve.
Rendering is a pure calculation
React calls your component functions to work out what the screen should look like. This step is called rendering. React expects a render to be a plain calculation: given props and state, return JSX. It does not expect a render to fail.
Rendering can still throw. Common causes:
- Reading a property of
undefined, such asuser.namewhenuserhas not loaded. - Calling
.map()on a value that is not an array. - Throwing on purpose when data is invalid.
function Profile({ user }) {
// Throws a TypeError if user is undefined.
return <h1>{user.name}</h1>;
}
function App() {
return (
<main>
<nav>My app</nav>
<Profile />
</main>
);
}App renders Profile without a user prop. Reading user.name throws a TypeError during rendering.
Why a try/catch around the component does not help
You might try to wrap the JSX in try/catch:
function App() {
try {
return (
<main>
<Profile />
</main>
);
} catch (error) {
return <p>Something went wrong.</p>;
}
}This does not work. Writing <Profile /> does not call Profile. It only creates a description of an element. React calls Profile later, after App has returned, so the catch block never sees the error. The throw happens inside React's own rendering work, outside any code you wrote.
What React does with the error
When a component throws during rendering and nothing handles the error, React unmounts the whole component tree. Here is the sequence:
- React starts rendering and calls
Profile. Profilethrows.- React looks upward for something that can handle the error.
- It finds nothing.
- React removes the entire tree from the screen.
The user sees a blank page. The nav, which was fine, disappears along with the broken part. State held in every component is lost.
React does this on purpose. After a rendering error, the UI may be in an inconsistent state. Showing wrong data, such as another user's name or a half-built form, is usually worse than showing nothing. React therefore removes the whole tree rather than keep a screen it cannot trust.
What you see in development and production
During development, React shows the error in the console. Many setups, such as Vite and Next.js, also draw an error overlay on top of the page. The overlay can hide the fact that production users get something different: a blank screen.
In production there is no overlay. The page goes empty, and the only trace is a message in the browser console, which users do not read. Without your own reporting, you may not learn about the failure at all.
The idea that fixes it
The problem is the size of the damage. One small component fails, and the whole app disappears. What you want is to limit the damage to the broken part and keep the rest of the page working.
React provides this through a special kind of component that sits above other components and catches errors thrown while they render. It can show a replacement message for its own subtree and leave everything outside it untouched. That component is called an error boundary, and the next chapter introduces it.
Summary
- Rendering can throw, for example when data is missing.
- A
try/catchin the parent does not catch errors from a child, because React calls the child later. - With no handler, React unmounts the whole tree and the user sees a blank page.
- React does this on purpose, to avoid showing a UI it cannot trust.
Exercise
A developer wraps return <Profile /> in a try/catch inside App, and Profile throws while rendering. Why does the catch block not run, and what does the user see?
Show answer
Writing <Profile /> only creates an element description. React calls Profile after App has returned, so the throw happens outside the try block. Nothing handles the error, so React unmounts the whole tree and the user sees a blank page.
Exercise
Name two reasons React removes the whole tree after an unhandled rendering error instead of leaving the last good screen in place.
Show answer
After a rendering error the UI may be inconsistent with the app's data. Showing wrong or half-built content, such as another user's data or a partly rendered form, can be worse than showing nothing. Also, state in the failed part of the tree cannot be trusted, so React discards it.