Sub topic 4
Showing a useful fallback
This chapter assumes you can write a class-based error boundary (the previous chapter) and know React props and JSX. It covers what the fallback should show once the boundary has caught an error.
What a fallback is for
When a boundary catches an error, it renders its fallback instead of the failed subtree. The fallback is the only thing the user sees from the failure, so it needs to do three jobs:
- Say that something went wrong, in plain words.
- Say what still works.
- Give the user a next step.
A fallback that renders null or the text "Error" does none of these. The user sees a blank gap and has no idea whether to wait, click, or reload.
A fallback component
Write the fallback as an ordinary component, so you can style and test it like any other.
function ProfileFallback() {
return (
<div role="alert" className="panel panel-error">
<h2>We couldn't show this profile</h2>
<p>The rest of the page still works. Reload the page or try again later.</p>
<a href="/">Back to the home page</a>
</div>
);
}The role="alert" attribute makes screen readers announce the message when it appears. Without it, a user who can't see the screen change gets no signal that the content was replaced.
function App() {
return (
<ErrorBoundary fallback={<ProfileFallback />}>
<Profile />
</ErrorBoundary>
);
}Fallbacks that depend on the error
A static element is enough for many cases. Sometimes the fallback needs the error object, for example to show a message during development. A common pattern is a renderFallback prop that receives the error.
import { Component } from "react";
class ErrorBoundary extends Component {
state = { error: null };
static getDerivedStateFromError(error) {
return { error };
}
render() {
const { error } = this.state;
if (error) {
const { fallback, renderFallback } = this.props;
return renderFallback ? renderFallback(error) : fallback;
}
return this.props.children;
}
}
function DetailedFallback({ error }) {
return (
<div role="alert">
<p>This section could not be loaded.</p>
{process.env.NODE_ENV !== "production" && <pre>{error.message}</pre>}
</div>
);
}
function Page() {
return (
<ErrorBoundary renderFallback={(error) => <DetailedFallback error={error} />}>
<Profile />
</ErrorBoundary>
);
}Showing error.message only outside production is deliberate. Error messages can contain file paths, request URLs, or fragments of data. Log the details (a later chapter covers reporting) and show the user a message you wrote.
Design rules
- Match the size of the failed content. A fallback much smaller than the content it replaces makes the page jump. Give it a similar height, such as a
min-heighton the panel. - Keep the language specific to the area. "We couldn't show this profile" is more useful than "Something went wrong".
- Offer a way forward. A link home, a support address, or a retry button all work. Retry needs a reset mechanism, which a later chapter builds.
- Keep it simple. The fallback should use little state and few dependencies.
Choosing the fallback's size
The right fallback depends on how much of the page failed.
A broken avatar needs a short line of text. A broken checkout needs a full panel that tells the user whether their cart is safe. The top-level fallback is the last resort, so keep it plain and dependency-free.
Exercise
A boundary around a dashboard chart uses the fallback <p>Error</p>. Name two changes that make this fallback more useful, and say why showing error.message to all users is risky.
Show answer
First, replace the text with a specific message, such as "We couldn't load the chart. The rest of the dashboard still works", and add a next step such as a link or a retry button. Second, add role="alert" and a min-height matching the chart, so screen readers announce the change and the layout doesn't jump. Showing error.message to every user is risky because messages can leak internal details such as file paths, URLs, or data. Show them only in development and send them to a logging service instead.