A user opens a note, starts typing, and the app closes. When they reopen it, the text they typed is gone.
I reproduced exactly that in a React Native app, with a crash reporter installed and configured. Then I opened the dashboard and found nothing. No crash, and the last five seconds of the session were missing.
This article covers how I set up crash reporting and session replay in that app. It also covers why the first crash never showed up, and the small component that fixed the gap. Everything below was run on a real release build on an Android emulator.
A stack trace tells you where. You also need why.
Most production bugs in mobile apps don't fail on the line that's wrong. They fail because of state the app didn't expect: a slow network, a background sync, a record another device deleted a second ago.
A crash report gives you the error, the stack trace, and device information. That's often enough to identify a missing null check, but it isn't enough when the real question is how the data ended up in that state. For that, you need to understand what happened leading up to the crash:
- Which screens the user went through.
- What they tapped.
- What network calls ran and what they returned.
- What your own code was doing, such as syncs, retries and cache updates.
That's what session replay gives you. One thing to be clear about: in most of the observability tools, and in the one I used here, session replay is not a video. It's a timeline of events (screen views, taps, HTTP calls, logs, lifecycle changes and your own custom events), plus screenshots and layout snapshots.
Basically you read it like a log that already knows which events belong to which user.
The app
SyncNote is a small offline-first notes app built with Expo SDK 58 and React Native 0.88. Notes lives in a local database on the phone (Turso), and the app syncs with the server in the background. While the app is open, it pulls changes from other devices every 5 seconds.
That background pull is the interesting part. It means the data under the current screen can change while the user is looking at it, which is a common source of bugs that are hard to reproduce.
The code is on GitHub.
Step 1: Install the SDK
I used Measure, an open-source tool for mobile crash reporting and session replay for production based applications. Measure's React Native SDK ships with an Expo config plugin, so you don't need to manually modify Android or iOS native files.
npx expo install @measuresh/react-native
Add the plugin to app.config.ts with the API key and URL from your Measure project:
// app.config.ts
plugins: [
['@measuresh/react-native', {
androidApiKey: process.env.MEASURE_API_KEY,
androidApiUrl: process.env.MEASURE_API_URL,
iosApiKey: process.env.MEASURE_API_KEY,
iosApiUrl: process.env.MEASURE_API_URL,
}],
],
The plugin writes the keys into the native project and starts the native SDKs when the app launches. On Android it also adds an OkHttp interceptor, which is what records HTTP calls. It edits native code, so you need a development build. Expo Go won't load it:
npx expo prebuild --platform android --clean
npx expo run:android
Then initialize the JavaScript side as early as possible, before your first screen renders:
// src/telemetry.ts
import { Measure, MeasureConfig } from '@measuresh/react-native';
await Measure.init({ config: new MeasureConfig({ enableLogging: __DEV__ }) });
Open the app and interact with a few screens. A session should appear in the dashboard within a minute. Since events are sent in batches, moving the app to the background can help trigger an earlier upload.
Step 2: Make release stack traces readable
Your release bundle is minified. Without a sourcemap, every frame in a JavaScript stack trace points to index.android.bundle at some column, which tells you nothing.
On Android, the Measure Gradle plugin handles this. It uploads the sourcemap whenever you build a release. You can see it in the build output:
> Task :app:rewriteReactNativeSourceMapRelease
> Task :app:packageReactNativeSourceMapRelease
> Task :app:uploadReleaseBuildToMeasure
The result is a stack trace that points to the file and line you wrote, like src/screens/NoteEditor.tsx:126:33. On iOS, you can upload script as a build phase instead. The symbol upload docs covers both.
Always test crashes in a release build. Debug builds handle errors differently, often displaying a red error screen instead of closing the app, while stack traces reference the development bundle rather than the production bundle.
Step 3: Record the events that explain your bugs
Automatic event covers screens, taps and network calls. They don't know what your app means by "sync" or "conflict". For that, track your own events, and keep them small: ids, counts and timings, never user content.
I wrapped every call in one file, so the event names stay consistent:
// src/telemetry.ts
export function track(name: EventName, attrs?: Attrs): void {
Measure.trackEvent({ name, attributes: clean(attrs) }).catch(() => {});
}
The event that ended up mattering most was the one I almost didn't add. It records what each background pull brought in:
// src/store.tsx, after a pull that changed something
track('remote_changes_received', {
notes_changed: changedNotes.length,
notes_removed: removed,
pull_ms: pullMs,
});
If your app modifies data without direct user interaction, whether through background sync, push notifications, WebSockets, or a cache refresh, log those changes. They often go unnoticed in crash reports but can be the underlying cause of a crash.
The bug
Here's the line that caused the crash. The note editor shows when the note was last edited:
// src/screens/NoteEditor.tsx (the buggy version)
const note = notes.find((n) => n.id === noteId);
{noteId && (
<Text style={styles.meta}>
Edited {timeAgo(note!.updated_at)} · rev {note!.rev}
</Text>
)}
It checks that a note id exists, then assumes the note does too. Usually that's true. It isn't when another device deletes the note while it's open here.
To reproduce it, I opened a note on the phone and started typing:
Then I deleted the same note from a second device, a script that writes straight to the server:
npm run remote:delete
About four seconds later, the next background pull removed the note from the list. The editor re-rendered with note set to undefined, and the app closed:
FATAL EXCEPTION: mqt_v_native
com.facebook.react.common.JavascriptException:
TypeError: Cannot read property 'updated_at' of undefined
The user's text was lost. It was never saved, because they hadn't tapped Save yet.
Why the crash never showed up
I gave it time, reopened the app so pending data could upload, and checked again. The session was there, but it stopped five seconds before the crash. The crash itself never arrived.
To understand why this happens, I went through the SDK's source code, followed by React Native's. The SDK captures JavaScript crashes by hooking into React Native's global error handler, ErrorUtils.setGlobalHandler, which is a common approach. Uncaught errors in timers, event handlers, and listeners pass through this handler. However, errors thrown during React's rendering process took a different path. React Native passes them directly to its exceptions manager:
// node_modules/react-native/Libraries/Core/ReactFiberErrorDialog.js
ExceptionsManager.handleException(error, false);
So the global handler is never called, and the crash reporter listening on it never sees the error. In a release build, the app then closes from the native side.
I checked the other path to confirm, in a debug build. I threw an error inside a setTimeout callback, and it arrived in the dashboard as a fatal crash, as expected. Errors from callbacks reach the handler. Errors from render don't.
Tested on React Native 0.88 with @measuresh/react-native 0.5.1. Run this check on your own stack: throw during render in a release build and see whether it reaches your dashboard.
Step 4: Handle React Rendering Errors Using an Error Boundary
React already has a tool for errors during render: an error boundary. It's a class component that catches errors in the components below it. Most guides use it to show a fallback screen. It's also the right place to report the error, because in this setup it's the one place your code gets to see it.
// src/components/ErrorBoundary.tsx (simplified)
export class ErrorBoundary extends React.Component<Props, State> {
state: State = { failed: false };
static getDerivedStateFromError(): State {
return { failed: true };
}
componentDidCatch(error: Error, info: React.ErrorInfo) {
Measure.trackError({
error,
attributes: {
source: 'error_boundary',
screen: this.props.screen,
component_stack: (info.componentStack ?? '').trim(),
},
});
}
render() {
if (!this.state.failed) return this.props.children;
return <Fallback onReset={this.reset} />;
}
}
I wrapped each screen in its own boundary, with a key, so a failure on one screen doesn't take down the rest of the app, and navigating away resets it:
// src/App.tsx (simplified)
<ErrorBoundary key={`editor-${noteId}`} screen="NoteEditor" onReset={toList}>
<NoteEditor noteId={noteId} onClose={toList} />
</ErrorBoundary>
I rebuilt the release with the boundary and the same bug, and ran the same steps. This time the app stayed opened and showed the fallback:
The error arrived in the dashboard with a readable stack trace:
TypeError: Cannot read property 'updated_at' of undefined
at NoteEditor (src/screens/NoteEditor.tsx:126:33)
at renderWithHooks (node_modules/react-native/.../ReactFabric-prod.js:3871:30)
Two things I didn't expect:
It's reported as a handled error, not a crash. The
trackErrormethod reports handled errors, which Measure doesn't actively sample by default. Session replay is also disabled for these errors. Head to the Adaptive Capture settings in the dashboard, increase the handled error sampling rate, and enable session replay. Otherwise, you might ship the error boundary without seeing the errors it catches.The component stack may remain minified in release builds. While the main JavaScript stack trace is symbolicated using source maps, the componentStack string provided by React may still contain minified references and bundle offsets. It's worth capturing because it can provide additional context about the component hierarchy where the error occurred.
Reading the session
Here's the session around that error, straight from the timeline:
| Time (UTC) | Event |
|---|---|
| 13:09:54.794 | Tap on "Product Launch Plan" |
| 13:09:54.832 | Screen view: NoteEditor
|
| 13:09:58.200 |
note_edited: the user starts typing |
| 13:10:06.695 | Background pull, HTTP 200 |
| 13:10:06.733 |
remote_changes_received with notes_removed: 1
|
| 13:10:06.743 |
TypeError in NoteEditor
|
| 13:10:20.620 | Tap on "Back to notes" |
The stack trace says note was undefined in the editor. The session explains why. The user was editing a note, a background pull removed exactly one note, and 10 milliseconds later the editor failed. The note under the open editor was deleted somewhere else.
None of that was in the error report. And without the remote_changes_received event, the timeline would have shown an HTTP 200 followed by an error, which tells you a lot less.
The fix
The one-line fix is to check note instead of noteId. But the session showed what the user was in the middle of, typing, so the real fix should also protect their text:
// src/screens/NoteEditor.tsx
const note = noteId ? notes.find((n) => n.id === noteId) : undefined;
const deletedElsewhere = !!noteId && !note;
useEffect(() => {
if (deletedElsewhere) {
track('note_deleted_remotely', { note_id: noteId!, had_unsaved_edit: editTracked.current });
}
}, [deletedElsewhere]);
// Save as a new note if the original is gone
await saveNote(deletedElsewhere ? null : noteId, title.trim(), content);
Now the editor tells the user what happened and keeps what they typed:
Tapping "Save as new note" keeps the text, and it syncs like any other note:
The new note_deleted_remotely event means that the next time this happens in production, it shows up in the data as a normal event rather than an error. had_unsaved_edit: true tells you how often users would have lost work.
What I'd tell anyone setting this up
- Test crash reporting in a release build. Debug builds handle errors differently and use a separate JavaScript bundle. An error that triggers a red screen during development may not be captured the same way in production. That's why it's important to test crash reporting in a release build.
- Throw an error during render on purpose and check that it arrives. Then throw one in a timer. If only one of them shows up, you've found the same gap I did.
-
Put an error boundary around each screen, and report from
componentDidCatch. You get the error, a fallback instead of a closed app, and the rest of the session keeps recording. - Upload sourcemaps as part of the release build, not as a manual step you'll forget.
- Log the state changes your users didn't make. Sync results, pushes and cache refreshes are what turn a session timeline from "something happened" into the cause.
- Check your sampling settings. Default settings for handled errors and replays are usually conservative to keep costs down, which is also how your most useful errors go missing.
Try it yourself
The app and every snippet above are in the GitHub repo. The repo has the fix. To bring the bug back, change {note && ( to {noteId && ( above the "Edited …" line in src/screens/NoteEditor.tsx. Then build a release, open a note, and run:
npm run remote:delete
Then compare what reaches your dashboard with and without the error boundary.
If you love my content ❤️ Follow me on LinkedIn
~ Kathank Raithatha







Top comments (0)