Sub topic 12
onCaughtError and onUncaughtError at the root
This chapter assumes you can write a class-based error boundary or use react-error-boundary. It also assumes you know that an error thrown during render either reaches a boundary or unmounts the tree. Here you will add one central place to observe both outcomes.
Why report at the root
Reporting from each boundary works. You can call your logger in componentDidCatch, or in the onError prop of react-error-boundary. This has two weaknesses:
- Every boundary needs the same logging code, and a new boundary can forget it.
- An error that no boundary catches never reaches any boundary, so none of them can report it.
React 19 adds two options to createRoot and hydrateRoot that cover both cases.
| Option | Called when | Typical meaning |
|---|---|---|
| `onCaughtError` | An error boundary caught the error | A section of the UI is showing a fallback |
| `onUncaughtError` | No boundary caught the error | The whole tree was unmounted |
Both receive (error, errorInfo). errorInfo.componentStack is a string describing the component path that led to the error. For caught errors, errorInfo.errorBoundary also gives the boundary that handled it.
Setting up both callbacks
Pass the callbacks as the second argument to createRoot.
import { createRoot } from "react-dom/client";
import App from "./App";
function sendToLogger(kind, error, componentStack) {
const payload = JSON.stringify({
kind,
message: error instanceof Error ? error.message : String(error),
stack: error instanceof Error ? error.stack : undefined,
componentStack,
url: window.location.href,
});
navigator.sendBeacon("/api/log-error", payload);
}
const root = createRoot(document.getElementById("root"), {
onCaughtError(error, errorInfo) {
sendToLogger("caught", error, errorInfo.componentStack);
},
onUncaughtError(error, errorInfo) {
sendToLogger("uncaught", error, errorInfo.componentStack);
},
});
root.render(<App />);navigator.sendBeacon suits this job. The browser queues the request even if the page is closing, and a failed log call cannot throw into your app.
The kind field lets you filter later. A rise in uncaught events usually means a missing boundary. A rise in caught events means users are seeing fallbacks, even though the app stays up.
What changes from React 18
In React 18, React re-threw errors that boundaries had caught. Browser tooling saw them twice, once from React and once from the global error handler. React 19 removed that duplication:
- Caught errors go to
console.errorby default. - Uncaught errors go to
window.reportErrorby default.
Setting a callback replaces the matching default. If you still want errors in the console during development, call console.error inside your callback.
onCaughtError(error, errorInfo) {
if (import.meta.env.DEV) {
console.error(error, errorInfo.componentStack);
}
sendToLogger("caught", error, errorInfo.componentStack);
}The import.meta.env.DEV check works in Vite. Use your bundler's equivalent otherwise.
Common mistakes
- Expecting these callbacks to see event handler errors. They report errors that React handles during rendering. An exception in an
onClickhandler, or after anawait, does not reach them. Route those errors withshowBoundaryas shown earlier, or handle them withtry/catch. OnceshowBoundaryforwards an error, the boundary catches it andonCaughtErrorfires. - Throwing inside a callback. A failing logger in
onUncaughtErrormakes a bad situation worse. Keep the callbacks small and guard network calls. - Logging in both places. If you keep
componentDidCatchlogging and also addonCaughtError, each caught error is reported twice. Pick one place. The root callback is easier to maintain. - Forgetting
hydrateRoot. Server-rendered apps usehydrateRoot(container, element, options), and the same two options go inoptions.
Exercise
Your app has one boundary around <Sidebar /> and none around <Checkout />. A component inside each throws during render. Which root callback runs for each, and what does the user see?
Show answer
The error in <Sidebar /> triggers onCaughtError, and the sidebar's fallback appears while the rest of the app keeps running. The error in <Checkout /> has no boundary above it, so onUncaughtError runs and React unmounts the whole tree, leaving a blank page.