Error Boundaries: The Pattern Every React App Should Use
An error boundary in React is a component that catches errors in its child component tree and renders a fallback UI instead of crashing the whole app. The pattern is built into React. Most apps still do not use it. The result is that an error in one widget can blank the entire page. The fix is a few lines of code per boundary plus a logging integration.
Written by Yashveer Singh, founder of Yashveer Labs.
What you actually need to know
- React error boundaries catch errors in the component tree and render fallbacks.
- Use them around routes, heavy widgets, and third party integrations.
- The App Router in Next.js provides error.tsx per route.
- Log to your error monitoring tool from componentDidCatch.
- Boundaries do not catch async or event handler errors.
| Boundary location | What it protects |
|---|---|
| App root | Catch all, prevents white screen |
| Route segment | Broken route does not break shell |
| Heavy widget | Chart, editor, integration |
| Third party integration | Vendor failure isolated |
| Marketing section | Decorative failure contained |
The core argument
React error boundaries are one of those built in features that most apps still do not use. The pattern is well documented. The implementation is small. The benefit is that an error in one part of the app does not take down the whole app. The cost of skipping is the white screen that customers report and the team learns about in support tickets.
The pattern is small. A component class with componentDidCatch and getDerivedStateFromError. The component wraps the part of the tree you want to protect. When a child throws during render, the boundary catches the error and renders a fallback. The rest of the page outside the boundary keeps working.
The Next.js App Router has made this even easier. An error.tsx file per route segment acts as an error boundary. The team gets per route protection without writing the boundary manually. The pattern is the default for new App Router projects.
The discipline is to use boundaries at the right granularity. The whole app gets one for catch all protection. Each route gets one so a broken route does not break the layout. Heavy widgets like charts get one because they often fail and the rest of the page should keep working. Third party integrations get one because vendor failures are common.
The other discipline is logging. The boundary catches the error. The team needs to know it happened. Send the error to Sentry or Bugsnag. The error monitoring tool aggregates and surfaces them. The team can prioritize the worst.
Where to place boundaries
| Location | Reason |
|---|---|
| App root | Catch all so the page never goes fully blank |
| Each route segment | A broken route renders a contained fallback |
| Each heavy widget | Chart, editor, complex component |
| Third party integrations | Vendor failures stay contained |
| Marketing decorations | Cosmetic failures do not break utility |
| Below the fold | Content the user might not see |
How much does this cost
The cost of adding boundaries is hours per surface. The cost of the logging integration is a few hours total. The total cost for a meaningful app is one or two sprints. The savings is the customer experience that does not break when a component fails.
Features the error handling must have
- Error boundaries at multiple granularities.
- Fallback UI that acknowledges the error and offers action.
- Logging to an error monitoring tool.
- Per route boundaries in App Router.
- Try catch around async code and event handlers.
- Telemetry on error boundary trip rate.
- A practice of testing the fallback path.
- A way to recover without full reload.
Expert opinion
Error boundaries are one of the highest leverage React patterns that most apps still skip. The implementation is small. The protection is real. The teams that use them ship apps that survive component failures. The teams that do not use them ship apps that white screen when a chart library throws an unexpected error. The discipline is to use boundaries by default and to fail gracefully at every level.
>
Yashveer Singh, founder of Yashveer Labs
How this played out on a real project
A client SaaS reported that some customers were seeing blank pages after a recent release. The investigation found a third party charting library that crashed under specific data conditions. The crash propagated up and React unmounted the whole tree.
We added error boundaries around every chart. The fallback was a small message and a retry button. The same data condition still caused the chart to fail but the rest of the page kept working. The blank page reports stopped.
We added more boundaries at the route level and around the heavy widgets. The error monitoring tool surfaced several errors that had been silent before. The team fixed the underlying issues. The customer experience improved meaningfully.
For more on the related work, see building forms that customers love and the patterns that make a SaaS feel premium.
Common mistakes teams make
- No error boundaries.
- One boundary at the app root only. Too coarse.
- No logging integration. Errors are invisible.
- Fallback is a stack trace. Frightens the user.
- Fallback offers no action. User has to refresh.
- Boundaries that swallow errors silently.
- No try catch around async code.
- Treating React's default error behavior as acceptable.
A two week plan to add boundaries
- Week one. Add app root boundary. Add per route boundaries. Integrate with error monitoring.
- Week two. Add boundaries around heavy widgets and third party integrations. Test the fallback paths.
For more on the related work, read the patterns that make a SaaS feel premium and crash reporting and mobile stability a bare minimum setup. On the broader reliability side, designing for failure a backend engineers mental model is the natural next read.
Frequently asked
Why this work lands with me
I am Yashveer Singh. Founder of Yashveer Labs. I take this kind of project because I have done enough of them to know what kills them. The version of me that writes a post like this is the same one who builds the system afterward. There is no handoff to a junior, no agency middleman, no surprise scope. That is the bet I am making on my own brand.
Posts that line up with this one.
- Web App and Frontend Development
Loading States, Skeletons, and Optimistic UI
How you handle loading states is one of the most visible indicators of product quality. Here is the decision framework for when to use spinners, skeletons, and optimistic updates, and the common mistakes that make apps feel slow.
- Web App and Frontend Development
Modal Patterns That Do Not Trap Users
Modals are overused, frequently misimplemented, and a common source of user frustration. Here is how to design and build modals that provide the right information at the right time without trapping users or creating accessibility failures.
- Web App and Frontend Development
Next.js vs Remix vs Astro vs Nuxt in 2026
Next.js, Remix, Astro, and Nuxt each make different architectural bets about how web applications should work. Here is how they compare in 2026 and which one belongs in which project.
- Web App and Frontend Development
React Query vs SWR vs RTK Query
React Query, SWR, and RTK Query all manage server state in React applications, but they make different trade-offs around complexity, bundle size, and Redux integration. Here is how to choose between them.