Steps to Integrate Sentry into the react-native application
Sentry Integration in Turbo Repo
Go to your
appsdirectory which contains all of your apps such as web, mobile, or any other kind of applications you've added to theappsdirectory.As you can see, there is a folder called
apps/mobilewhich is a react-native applicationI switched to the
apps/mobilefolder in where I was trying to install the@sentry/react-nativeSDK into my react native application.For that I follow the below steps: (macOS - MacBook Air)
- 4.1. I opened the terminal in my mac (You can use any other terminals you want or go with the
vscodeterminal) - 4.2. Type the
cd apps/mobilecommand in the terminal and click on theenter/returnbutton - 4.3. Type the
yarn add @sentry/react-nativecommand in the terminal and click on theenter/returnbutton- 4.3.1. If you are using the
npmpackage installer, use thenpm install --save @sentry/react-nativecommand (but in this repo, it won't work because it is a turbo repo and uses theyarn) - For more info, check the Sentry official documentation: "https://docs.sentry.io/platforms/react-native/"
- 4.3.1. If you are using the
- 4.4. After the installation was successful, I put the below code in
apps/mobile/mobile-screens/index.js
import {AppRegistry} from 'react-native';
import App from './mobile-screens';
import {name as appName} from './app.json';
import {Provider} from 'react-redux';
import {mobileStore} from 'store';
import React from 'react'
import Config from 'react-native-config';
// Sentry Code: import & initiate sentry in the App
import * as Sentry from "@sentry/react-native";
Sentry.init({
dsn: Config.SENTRY_DSN, // put SENTRY_DSN env variable in .env, otherwise it fails
});
// above App component
const App = () => {
....other code
}
// Sentry Code: wrap the App component with a sentry `wrap` HOC function
export default Sentry.wrap(App);- 4.5. Make changes in
apps/mobile/index.jswith the following code
import { AppRegistry } from "react-native";
import App from "./mobile-screens";
import { name as appName } from "./app.json";
import { Provider } from "react-redux";
import { mobileStore } from "store";
import React from "react";
// Sentry Code: Import sentry
import * as Sentry from "@sentry/react-native";
const MobileApp = () => {
return (
// Sentry Code: Wrap the App with "Sentry.TouchEventBoundary"
<Sentry.TouchEventBoundary>
<Provider store={mobileStore}>
<App />
</Provider>
</Sentry.TouchEventBoundary>
);
};
AppRegistry.registerComponent(appName, () => MobileApp);- 4.1. I opened the terminal in my mac (You can use any other terminals you want or go with the
How to use?
- Refer this documentation to know more: "https://docs.sentry.io/platforms/react-native/usage/"
- To test whether the sentry is working or not in the app, I have added the below code in the
apps/mobile/mobile-screens/index.js->init()function
const init = async () => {
try {
// error will come because of the comment in the below code
// let tokenFromStorage = await AsyncStorage.getItem('@access_token');
if (tokenFromStorage !== null) {
dispatch(setTokens(JSON.parse(tokenFromStorage)));
let userProfile = await getUser();
if (userProfile) {
dispatch(setUser(userProfile));
}
}
} catch (error) {
// this will execute and will be shown in the Sentry dashboard
Sentry.captureException(error);
}
};


