From 88d55eb409b5542c285214940a456b4dc9fbb320 Mon Sep 17 00:00:00 2001 From: Alexander Pantiukhov Date: Mon, 20 Apr 2026 14:35:06 +0200 Subject: [PATCH] docs(react-native): Document GlobalErrorBoundary for fatal non-render errors Adds a "Showing a Fallback UI for Fatal Errors" subsection covering Sentry.GlobalErrorBoundary (and the withGlobalErrorBoundary HOC), the includeNonFatalGlobalErrors and includeUnhandledRejections opt-ins, reset semantics, and a migration note replacing the hand-rolled ErrorUtils.setGlobalHandler + DeviceEventEmitter + ErrorBoundary pattern the page previously recommended. Updates the top-of-page warning and the Non-Render Errors intro to link to the new section. The companion SDK change adding the component ships in getsentry/sentry-react-native#6023. --- .../integrations/error-boundary.mdx | 136 +++++++++++------- 1 file changed, 84 insertions(+), 52 deletions(-) diff --git a/docs/platforms/react-native/integrations/error-boundary.mdx b/docs/platforms/react-native/integrations/error-boundary.mdx index 9ac7fbdf474c93..10accf0218072b 100644 --- a/docs/platforms/react-native/integrations/error-boundary.mdx +++ b/docs/platforms/react-native/integrations/error-boundary.mdx @@ -12,7 +12,7 @@ The React Native SDK exports an error boundary component that uses [React compon -React error boundaries **only catch errors during rendering, in lifecycle methods, and in constructors**. They do **not** catch errors in event handlers, asynchronous code (`setTimeout`, `Promise`), or native errors. See [Handling Non-Render Errors](#handling-non-render-errors) for how to handle those. +React error boundaries **only catch errors during rendering, in lifecycle methods, and in constructors**. They do **not** catch errors in event handlers, asynchronous code (`setTimeout`, `Promise`), or native errors. For a fallback UI that covers those cases too, use [`Sentry.GlobalErrorBoundary`](#showing-a-fallback-ui-for-fatal-errors). @@ -102,7 +102,7 @@ In [React v17 and above](https://reactjs.org/blog/2020/08/10/react-v17-rc.html#n Errors in event handlers, `async` functions, `setTimeout`, and other non-render code won't be caught by `ErrorBoundary`. This is a [React limitation](https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary), not specific to Sentry. -Sentry's [`reactNativeErrorHandlersIntegration`](/platforms/react-native/integrations/default/#reactnativeerrorhandlersintegration) (enabled by default) **automatically reports** these errors to Sentry — you don't need to do anything extra for error reporting. However, it won't display a fallback UI. +Sentry's [`reactNativeErrorHandlersIntegration`](/platforms/react-native/integrations/default/#reactnativeerrorhandlersintegration) (enabled by default) **automatically reports** these errors to Sentry — you don't need to do anything extra for error reporting. For a fallback UI on fatal non-render errors, use [`Sentry.GlobalErrorBoundary`](#showing-a-fallback-ui-for-fatal-errors). ### Component-Level Error Handling @@ -138,74 +138,94 @@ function MyComponent() { } ``` -### Global Error Handling With Fallback UI +### Showing a Fallback UI for Fatal Errors -To show a fallback UI for **any** unhandled JavaScript error (not just render errors), you can create a provider that listens to React Native's global error handler and combines it with `ErrorBoundary`: +To show a fallback UI for fatal JavaScript errors that are thrown **outside** the React render tree — event handlers, `setTimeout`, async code, and errors routed through `ErrorUtils` — wrap your app in `Sentry.GlobalErrorBoundary`: ```javascript -import React, { useState, useEffect } from "react"; -import { Button, DeviceEventEmitter, Text, View } from "react-native"; +import React from "react"; +import { Button, Text, View } from "react-native"; import * as Sentry from "@sentry/react-native"; -// Set up the global error listener before Sentry.init() -const globalHandler = global.ErrorUtils?.getGlobalHandler(); -global.ErrorUtils?.setGlobalHandler((error, isFatal) => { - DeviceEventEmitter.emit("GLOBAL_UNHANDLED_ERROR", error); - - // Call the default handler in development for the React Native red box. - // In production, we skip the default handler to prevent the app from - // crashing so the fallback UI can be shown instead. - if (__DEV__ && globalHandler) { - globalHandler(error, isFatal); - } -}); - -Sentry.init({ - dsn: "___PUBLIC_DSN___", -}); - -function GlobalFallback({ onReset }) { +function App() { return ( - - Something went wrong. -