Sentry for React Native
Learn how to set up Sentry's React Native SDK and capture your first events.
Read on to find out how to set up Sentry's React Native SDK, which automatically reports errors and exceptions in your application. If you prefer video instructions, see How to Install the Sentry React Native SDK in 60 Seconds.
If you don't already have an account and Sentry project, head over to sentry.io, then return to this page.
Sentry captures data by using an SDK within your application's runtime. These platform-specific SDKs give Sentry a deeper understanding of how your application works.
Run @sentry/wizard to install and configure the SDK:
npx @sentry/wizard@latest -i reactNative
npx @sentry/wizard@latest -i reactNative
Sentry Wizard patches your project. You only need to do this once; then add the patched files to version control. If you prefer, follow the manual setup guide.
If you're using Expo, follow the Expo guide for Expo-specific setup. The SDK works with both managed and bare projects.
Error Monitoring starts working as soon as you call Sentry.init(). Logs need enableLogs: true in the initialization snippet below, and you'll learn how to send them in Send Logs. Application Metrics need no extra configuration, and you'll learn how to send them in Send Application Metrics.
This guide also enables Tracing, Profiling, Session Replay, and User Feedback. Size Analysis uses a separate build-time setup; see Size Analysis.
To capture errors, initialize the Sentry React Native SDK as early as possible in your application's lifecycle.
The snippet also turns on:
- Tracing: via
tracesSampleRate - Profiling: via
profilesSampleRate(see Profiling) - Session Replay: via
mobileReplayIntegration()and the replay sample rates (see Session Replay) - User Feedback: via
feedbackIntegration()(see User Feedback) - Logs: via
enableLogs
Don't need a feature? Remove its integration and options from the snippet.
App.jsimport * as Sentry from "@sentry/react-native";
Sentry.init({
dsn: "___PUBLIC_DSN___",
// Adds context data to events, such as IP address, cookies, and user.
sendDefaultPii: true,
integrations: [
// Enables Session Replay
Sentry.mobileReplayIntegration(),
// Enables User Feedback. Open the form with Sentry.showFeedbackWidget().
Sentry.feedbackIntegration(),
],
// Enables Tracing.
// Set to 1.0 to capture 100% of spans for tracing.
// We recommend reducing this value in production.
tracesSampleRate: 1.0,
// Enables Profiling for 100% of sampled transactions.
profilesSampleRate: 1.0,
// Enables Logs to be sent to Sentry.
enableLogs: true,
// Capture replays for 10% of sessions and 100% of sessions with an error.
replaysSessionSampleRate: 0.1,
replaysOnErrorSampleRate: 1.0,
});
import * as Sentry from "@sentry/react-native";
Sentry.init({
dsn: "___PUBLIC_DSN___",
// Adds context data to events, such as IP address, cookies, and user.
sendDefaultPii: true,
integrations: [
// Enables Session Replay
Sentry.mobileReplayIntegration(),
// Enables User Feedback. Open the form with Sentry.showFeedbackWidget().
Sentry.feedbackIntegration(),
],
// Enables Tracing.
// Set to 1.0 to capture 100% of spans for tracing.
// We recommend reducing this value in production.
tracesSampleRate: 1.0,
// Enables Profiling for 100% of sampled transactions.
profilesSampleRate: 1.0,
// Enables Logs to be sent to Sentry.
enableLogs: true,
// Capture replays for 10% of sessions and 100% of sessions with an error.
replaysSessionSampleRate: 0.1,
replaysOnErrorSampleRate: 1.0,
});
To automatically instrument your app with touch event tracking and automatic tracing, wrap its root component with Sentry.wrap:
App.jsexport default Sentry.wrap(App);
export default Sentry.wrap(App);
You don't need this if your app doesn't have a single parent App component.
Use Sentry.logger to send logs at different levels. You can include attributes to search or filter by later.
The SDK can capture console.* calls when Logs are enabled. See the Console logging integration for details.
Sentry.logger.info("Order processed", { order_id: "order-123" });
Sentry.logger.warn("Slow operation detected", {
operation: "data_fetch",
duration_ms: 3500,
});
Sentry.logger.info("Order processed", { order_id: "order-123" });
Sentry.logger.warn("Slow operation detected", {
operation: "data_fetch",
duration_ms: 3500,
});
See Set Up Logs for all available logging methods and message formatting options.
Application Metrics are supported in React Native SDK 7.8.0 and newer. No extra option is needed after SDK initialization. Use Sentry.metrics to send counters, gauges, and distributions:
Sentry.metrics.count("checkout.failed", 1);
Sentry.metrics.gauge("active_connections", 42);
Sentry.metrics.distribution("api_latency", 187, { unit: "millisecond" });
Sentry.metrics.count("checkout.failed", 1);
Sentry.metrics.gauge("active_connections", 42);
Sentry.metrics.distribution("api_latency", 187, { unit: "millisecond" });
See Set Up Metrics for all available metric types and configuration options.
Trigger a test error to confirm that Sentry is receiving events. You should see the error in Sentry within a few minutes.
throw new Error("My first Sentry error!");
throw new Error("My first Sentry error!");
Sentry.nativeCrash();
Alternatively, use the Sentry Playground to test your setup interactively with multiple error scenarios.
Head over to your project on Sentry.io to view the captured error.
At this point, you should have integrated Sentry into your React Native application. Here's what to explore next:
- Explore practical guides on what to monitor, log, track, and investigate after setup
- Explore React Native features
- Add readable stack traces to errors
- Add the Apple Privacy Manifest
- Troubleshoot your setup
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").