DEV Community

talesmgodois
talesmgodois

Posted on

Managing Environments in Sentry for React Native

Managing Environments in Sentry for React Native

When working with Sentry in a React Native project, properly managing environments (e.g., development, staging, and production) is crucial. This ensures that errors are tracked in the correct context, preventing noise in production and helping teams debug effectively.

Why Use Environments in Sentry?

By setting up different environments, you can:

  • Separate production errors from development issues.
  • Track bugs specific to staging environments.
  • Configure different alerting rules for each environment.
  • Improve debugging by isolating crashes based on release versions.

Setting Up Environments in Sentry for React Native

1. Install & Configure Sentry in Your Project

If you haven’t already installed Sentry in your React Native app, you can do so with:

npm install --save @sentry/react-native
# or
yarn add @sentry/react-native
Enter fullscreen mode Exit fullscreen mode

Link Sentry to your project:

npx @sentry/wizard -i reactNative -p ios android
Enter fullscreen mode Exit fullscreen mode

2. Define the Environment in Your Sentry Configuration

Modify your Sentry initialization in index.js or App.js:

import * as Sentry from "@sentry/react-native";

Sentry.init({
  dsn: "YOUR_SENTRY_DSN",
  environment: __DEV__ ? "development" : "production",
  enableNative: true, // Enables native crash reporting
});
Enter fullscreen mode Exit fullscreen mode

This automatically assigns:

  • development when running locally (__DEV__ is true).
  • production when building a release (__DEV__ is false).

3. Use Environment Variables for More Control

Instead of relying on __DEV__, you can define environments using .env files.

Install react-native-dotenv:

npm install react-native-dotenv --save-dev
Enter fullscreen mode Exit fullscreen mode

Create an .env file:

SENTRY_ENV=staging
Enter fullscreen mode Exit fullscreen mode

Update babel.config.js:

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [['module:react-native-dotenv']],
};
Enter fullscreen mode Exit fullscreen mode

Then modify your Sentry initialization:

import * as Sentry from "@sentry/react-native";
import { SENTRY_ENV } from "@env";

Sentry.init({
  dsn: "YOUR_SENTRY_DSN",
  environment: SENTRY_ENV,
});
Enter fullscreen mode Exit fullscreen mode

4. Verifying and Testing

To check if Sentry is capturing events correctly, trigger a test error:

Sentry.captureException(new Error("Test error from React Native"));
Enter fullscreen mode Exit fullscreen mode

Then go to Sentry Dashboard > Issues and verify that the error appears under the expected environment.

5. Setting Up Alerts per Environment

In Sentry, navigate to Alerts and create custom rules based on environments:

  • Production: Notify only critical issues.
  • Staging: Notify the QA team.
  • Development: Log issues but avoid alerts.

Conclusion

Setting up environments in Sentry for React Native helps teams separate logs, prioritize debugging, and reduce noise in production monitoring. With environment-based alerting and proper tracking, you can effectively manage errors in your React Native app.

Happy coding! 🚀

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs