|
| 1 | +# @exceptionless/react-native |
| 2 | + |
| 3 | +Use for React Native and Expo apps. This package adds a React Native client, AsyncStorage-backed persistence, React Native/Hermes stack parsing, lifecycle handling, global JavaScript error capture, an error boundary, and iOS native crash reporting. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +Expo: |
| 8 | + |
| 9 | +```bash |
| 10 | +npx expo install @exceptionless/react-native @react-native-async-storage/async-storage |
| 11 | +``` |
| 12 | + |
| 13 | +React Native CLI: |
| 14 | + |
| 15 | +```bash |
| 16 | +npm install @exceptionless/react-native @react-native-async-storage/async-storage |
| 17 | +cd ios && pod install |
| 18 | +``` |
| 19 | + |
| 20 | +For Expo development or standalone builds, add the config plugin: |
| 21 | + |
| 22 | +```json |
| 23 | +{ |
| 24 | + "expo": { |
| 25 | + "plugins": ["@exceptionless/react-native/expo-plugin"] |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +Native iOS crash reporting requires an Expo development build, a standalone build, or a bare React Native app. Expo Go can capture JavaScript errors but cannot load the native crash reporter. |
| 31 | + |
| 32 | +## Configure |
| 33 | + |
| 34 | +Call `startup` once during app initialization: |
| 35 | + |
| 36 | +```tsx |
| 37 | +import { Exceptionless } from "@exceptionless/react-native"; |
| 38 | + |
| 39 | +await Exceptionless.startup((config) => { |
| 40 | + config.apiKey = "API_KEY_HERE"; |
| 41 | + config.setUserIdentity("12345678", "Blake"); |
| 42 | + config.defaultTags.push("React Native"); |
| 43 | +}); |
| 44 | +``` |
| 45 | + |
| 46 | +For self-hosted Exceptionless: |
| 47 | + |
| 48 | +```tsx |
| 49 | +await Exceptionless.startup((config) => { |
| 50 | + config.apiKey = "API_KEY_HERE"; |
| 51 | + config.serverUrl = "https://exceptionless.example.com"; |
| 52 | +}); |
| 53 | +``` |
| 54 | + |
| 55 | +For local simulator development, prefer `http://localhost:<port>` when the app is running in the iOS simulator. Start Expo with `--localhost` or set `REACT_NATIVE_PACKAGER_HOSTNAME=localhost` so Metro bundle URLs in stack traces also use localhost. Use a LAN IP only when a physical device must reach a server on the development machine. |
| 56 | + |
| 57 | +## Error Boundary |
| 58 | + |
| 59 | +Wrap rendering surfaces to capture React render errors and attach the React component stack to `@error.data["@component_stack"]`: |
| 60 | + |
| 61 | +```tsx |
| 62 | +import { Text } from "react-native"; |
| 63 | +import { ExceptionlessErrorBoundary } from "@exceptionless/react-native"; |
| 64 | + |
| 65 | +export function App() { |
| 66 | + return ( |
| 67 | + <ExceptionlessErrorBoundary fallback={<Text>Something went wrong.</Text>}> |
| 68 | + <YourApp /> |
| 69 | + </ExceptionlessErrorBoundary> |
| 70 | + ); |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +React error boundaries do not catch event handlers, async failures, or manually swallowed errors. Submit those explicitly. |
| 75 | + |
| 76 | +## Send |
| 77 | + |
| 78 | +```tsx |
| 79 | +import { Exceptionless, toError } from "@exceptionless/react-native"; |
| 80 | + |
| 81 | +try { |
| 82 | + await saveProfile(); |
| 83 | +} catch (error) { |
| 84 | + await Exceptionless.submitException(toError(error)); |
| 85 | +} |
| 86 | + |
| 87 | +await Exceptionless.submitLog("mobile", "Profile opened", "info"); |
| 88 | +await Exceptionless.submitFeatureUsage("Profile Editor"); |
| 89 | + |
| 90 | +await Exceptionless.createException(new Error("Checkout failed")) |
| 91 | + .addTags("checkout", "mobile") |
| 92 | + .setProperty("orderId", "12345") |
| 93 | + .markAsCritical(true) |
| 94 | + .submit(); |
| 95 | +``` |
| 96 | + |
| 97 | +## Captured Behavior |
| 98 | + |
| 99 | +- Unhandled JavaScript errors and unhandled promise rejections are captured automatically after startup. |
| 100 | +- React Native/Hermes stack frames are parsed into structured Exceptionless stack frames. |
| 101 | +- iOS native crashes are persisted by PLCrashReporter and submitted on the next launch. |
| 102 | +- Device, OS, locale, React Native version, sessions, and lifecycle state are captured when available. |
| 103 | +- Event queue storage uses `@react-native-async-storage/async-storage`. |
| 104 | + |
| 105 | +## Troubleshooting |
| 106 | + |
| 107 | +- If native crashes do not appear in Expo, verify the app is not running in Expo Go and that the config plugin is present before rebuilding the native app. |
| 108 | +- If simulator submissions cannot reach a local Exceptionless server, use `http://localhost:<port>` for iOS Simulator and make sure Metro is not running in Expo's default LAN mode. Physical devices need a reachable LAN host. |
| 109 | +- For malformed or unexpected stacks, verify behavior in `ReactNativeErrorPlugin` tests before changing parser logic. |
| 110 | +- For native crash report loss concerns, verify `NativeCrashPlugin` only clears pending reports after at least one report is retrieved and submitted. |
| 111 | + |
| 112 | +## Source Anchors |
| 113 | + |
| 114 | +- `packages/react-native/README.md` |
| 115 | +- `packages/react-native/src/ReactNativeExceptionlessClient.ts` |
| 116 | +- `packages/react-native/src/ExceptionlessErrorBoundary.tsx` |
| 117 | +- `packages/react-native/src/plugins/ReactNativeErrorPlugin.ts` |
| 118 | +- `packages/react-native/src/plugins/ReactNativeGlobalHandlerPlugin.ts` |
| 119 | +- `packages/react-native/src/plugins/ReactNativeLifeCyclePlugin.ts` |
| 120 | +- `packages/react-native/src/plugins/NativeCrashPlugin.ts` |
| 121 | +- `packages/react-native/src/storage/AsyncStorageProvider.ts` |
| 122 | +- `packages/react-native/exceptionless-react-native.podspec` |
| 123 | +- `packages/react-native/expo-plugin/withExceptionless.cjs` |
| 124 | +- `example/expo/` |
0 commit comments