Skip to content

Commit 3e11c08

Browse files
AbhiPrasadvivianyentran
authored andcommitted
feat(react): Document Sentry.reactErrorHandler (#10178)
Co-authored-by: vivianyentran <[email protected]>
1 parent 7676dc1 commit 3e11c08

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

platform-includes/getting-started-config/javascript.react.mdx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,29 @@ root.render(<App />);
5656

5757
Once this is done, all unhandled exceptions are automatically captured by Sentry.
5858

59+
### React 19 Error Reporting
60+
61+
Starting with React 19, the `createRoot` and `hydrateRoot` methods from `react-dom` expose error hooks that are used to capture errors automatically. If you want to customize how errors are handled in specific error hooks, you can use the `Sentry.reactErrorHandler` function.
62+
63+
```javascript
64+
import { createRoot } from "react-dom/client";
65+
66+
const container = document.getElementById(“app”);
67+
const root = createRoot(container, {
68+
// Callback called when an error is thrown and not caught by an ErrorBoundary.
69+
onUncaughtError: Sentry.reactErrorHandler((error, errorInfo) => {
70+
console.warn('Uncaught error', error, errorInfo.componentStack);
71+
}),
72+
// Callback called when React catches an error in an ErrorBoundary.
73+
onCaughtError: Sentry.reactErrorHandler(),
74+
// Callback called when React automatically recovers from errors.
75+
onRecoverableError: Sentry.reactErrorHandler(),
76+
});
77+
root.render();
78+
```
79+
80+
These hooks apply to all React components mounted to the container passed onto `createRoot`/`hydrateRoot`. If you want more finely grained control over error handling, we recommend adding an `ErrorBoundary` component to your application.
81+
5982
### Add Error Boundary
6083

6184
If you're using React 16 or above, you can use the [Error Boundary](features/error-boundary/) component to automatically send Javascript errors from inside a component tree to Sentry, and set a fallback UI.

0 commit comments

Comments
 (0)