A short circuit in the kitchen, and the whole house goes dark, because there's only one circuit breaker for every room. With a breaker per room, only the kitchen goes off, and the other rooms stay lit.
An app can have a "breaker" per part too. That's called graceful degradation: when one part of an app breaks, the other parts keep working, and the broken part shows a polite stand-in. In React, the tool for this is the error boundary: a component that catches errors thrown by the components inside it while they render, and shows a fallback (a replacement view) instead of turning the whole page blank.
Error boundary: a catcher for render errors
class ErrorBoundary extends React.Component {
state = { hasError: false }
static getDerivedStateFromError(error) {
return { hasError: true } // the next render shows the fallback
}
componentDidCatch(error, info) {
logError(error, info) // send it to monitoring, Sentry for example
}
render() {
if (this.state.hasError) return this.props.fallback
return this.props.children
}
}
<ErrorBoundary fallback={<p>[Chart unavailable]</p>}>
<Chart />
</ErrorBoundary>
- React calls
getDerivedStateFromErrorwhen a child throws while rendering. The state it returns makes the boundary showfallback. componentDidCatchis where you report the error, so the team knows something broke.- An error boundary still has to be a class component; even React 19 has no hook version of it. If you'd rather not write one, the
react-error-boundarylibrary gives you a ready-made component.
One at the root or one per widget
// One boundary at the root
<ErrorBoundary fallback={<p>Something went wrong</p>}>
<Header /> <Chart /> <Footer />
</ErrorBoundary>
// One boundary per widget
<ErrorBoundary fallback={<p>[Header unavailable]</p>}><Header /></ErrorBoundary>
<ErrorBoundary fallback={<p>[Chart unavailable]</p>}><Chart /></ErrorBoundary>
<ErrorBoundary fallback={<p>[Footer unavailable]</p>}><Footer /></ErrorBoundary>
| Placement | Chart throws | What the user sees |
|---|---|---|
| One at the root | yes | only "Something went wrong", Header and Footer are gone too |
| Per widget | yes | "Welcome", "[Chart unavailable]", "© 2026" |
With a boundary only at the root, one broken widget wipes out everything: Header and Footer disappear too, though they did nothing wrong. A boundary per widget keeps the "blast" small. A boundary at the root is still useful as the last safety net, for errors that slip past every other boundary.
What an error boundary doesn't catch
An error boundary only catches errors that happen while React is rendering the components inside it. What slips through:
- Errors in event handlers, such as
onClick. - Async code:
setTimeoutcallbacks, promises,fetch. - Server-side rendering (SSR).
- Errors inside the boundary itself (rather than in its children).
Since React 19 there are two exceptions that still reach the nearest boundary: an error in the function passed to startTransition, even when it is called from onClick, and a rejected promise read with use().
For event handlers and async code, catch them yourself with try/catch, keep the message in state, then show it:
async function handleSave() {
try {
await saveDraft()
} catch (e) {
setError(e.message) // the boundary will never know, so we show it ourselves
}
}
Graceful degradation without React: safelyRender
The idea is the same in plain code: put a guard per part, not one for everything. This is what you write in the exercise:
function safelyRender(widgets, log) {
return widgets.map((widget) => {
try {
return widget.render()
} catch (e) {
log({ widget: widget.name, error: e.message })
return "[" + widget.name + " unavailable]"
}
})
}
| Widget | render() |
Result | What log records |
|---|---|---|---|
| Header | "Welcome" |
Welcome |
|
| Chart | throws Error("API down") |
[Chart unavailable] |
{ widget: "Chart", error: "API down" } |
| Footer | "© 2026" |
© 2026 |
- The
try/catchsits insidemap, so every widget has its own guard. Footer still renders even though Chart failed. logis called with an object{ widget, error }, anderrorholdse.message(the textAPI down), not the error object.- The fallback names the widget:
"[" + widget.name + " unavailable]".
Data from many APIs: Promise.all vs Promise.allSettled
A page that calls three APIs at once (user, posts, notifications) has the same problem. Promise.all rejects as soon as one promise fails, so one dead API makes the whole page fail. Promise.allSettled waits for all of them to finish and never rejects. Its result is an array of { status: "fulfilled", value } or { status: "rejected", reason }, in the same order as the promises.
async function allSettledFallback(promises, fallbacks) {
const results = await Promise.allSettled(promises)
return results.map((r, i) => (r.status === "fulfilled" ? r.value : fallbacks[i]))
}
allSettledFallback(
[Promise.resolve("user"), Promise.reject("err"), Promise.resolve("notif")],
[null, [], null],
).then((result) => {
console.log(result[0]) // user
console.log(JSON.stringify(result[1])) // []
console.log(result[2]) // notif
})
- A failed one is replaced by
fallbacks[i], the fallback in the same position, so the order never shifts. - Its two siblings have other jobs:
Promise.racetakes the first to finish (success or failure),Promise.anytakes the first success.
🎯 Principles teams use
- Fail visibly, not silently. Show the user a fallback, and report the error to monitoring from
componentDidCatch. - Human messages. "The chart didn't load. Try again?" is clearer than "AxiosError: 500".
- Don't throw away the user's data. A draft being typed is kept even when the Save button fails.
- Let it recover. A Try again button that resets the boundary (
setState({ hasError: false })) saves the user a page reload.
⚠️ Why one error can still wipe out everything
try/catcharound the whole loop. As soon as Chart fails, the loop stops and Footer never renders. The guard belongs inside, per widget.- Logging the error object.
log({ widget, error: e })makescalls[0].erroran Error object, not the textAPI down. Usee.message. - Dropping the failed results. Filtering to the fulfilled ones only makes
result[1]becomenotif, when it must be[]. Replace them with the fallback at the same index. - Only one boundary, at the root. The app doesn't go blank, but one broken widget still wipes out the whole page.
TL;DR: An error boundary (a class component with getDerivedStateFromError and componentDidCatch) catches render errors in its child components and shows a fallback. Put one per widget or section, plus one at the root as the last safety net, so a broken Chart only replaces itself with [Chart unavailable]. A boundary doesn't catch errors in event handlers, async code, SSR or itself (except, since React 19, errors in startTransition and a promise read with use()): catch those with try/catch and keep them in state. In plain code, safelyRender puts a try/catch inside map, logs { widget: name, error: e.message }, and returns "[Name unavailable]". For many APIs, Promise.allSettled waits for all of them and never rejects, and a failed one is replaced by the fallback in the same position, so the result is user, [], notif.
⚡ One breaker per part
Meaning: Graceful degradation: when one part of an app breaks, the other parts keep working, and the broken part shows a polite stand-in. In React, the tool is the error boundary: a component that catches render errors in its child components, then shows a fallback.
⚡ One per widget, not just at the root
Write:
<ErrorBoundary fallback={<p>[Chart unavailable]</p>}><Chart /></ErrorBoundary>
Guess first: there's only one boundary, at the root, and Chart throws. What happens to Header and Footer?
They disappear too. The user only sees "Something went wrong". With a boundary per widget, only Chart is replaced by [Chart unavailable].
Meaning: A boundary at the root is still useful as the last safety net.
⚡ What a boundary doesn't catch
Guess first: does an error boundary catch an error in onClick?
No. A boundary only catches errors while React is rendering. Errors in event handlers, async code (setTimeout, promises, fetch), SSR and the boundary itself slip through. The exceptions since React 19: an error inside startTransition and a promise read with use().
Meaning: For those, catch them yourself with try/catch, keep the message in state, then show it.
⚡ Many APIs: Promise.allSettled
Write:
const results = await Promise.allSettled(promises)
return results.map((r, i) => (r.status === "fulfilled" ? r.value : fallbacks[i]))
Meaning: Promise.all rejects as soon as one fails. Promise.allSettled waits for all of them and never rejects, so a failed one can be replaced by the fallback in the same position.