|
| 1 | +--- |
| 2 | +title: React Component Names |
| 3 | +excerpt: "" |
| 4 | +description: "Learn how Sentry's React Native SDK allows you to monitor your components." |
| 5 | +sidebar_order: 57 |
| 6 | +--- |
| 7 | + |
| 8 | +You can set up Sentry's React Native SDK to use React component names instead of element names. So instead of looking at generic names like this: |
| 9 | + |
| 10 | + |
| 11 | +``` |
| 12 | +View > Touchable > View > Text |
| 13 | +``` |
| 14 | + |
| 15 | +You can see exactly which React component was used, as in the example below: |
| 16 | + |
| 17 | +```html |
| 18 | +MyCard (View, MyCard.ts) > MyButton (Touchable, MyCard.ts) > View > Text |
| 19 | +``` |
| 20 | +Once you've enabled capturing, you'll be able to see the specific name of the component that was interacted with in breadcrumbs and spans. This removes the ambiguity of having to guess by looking at a generic name, which becomes more difficult the larger your application is. |
| 21 | + |
| 22 | +We're working to release more features that will leverage component name capturing in the future and highly recommended that you configure your project to use it. |
| 23 | + |
| 24 | +## Prerequisites |
| 25 | + |
| 26 | +- [Install](/platforms/react-native/) React Native SDK `5.25.0-alpha.3` or newer. |
| 27 | +- Ensure the React components you want to capture are in `.jsx` or `.tsx` file formats. |
| 28 | + |
| 29 | +## Enable Component Name Capturing |
| 30 | + |
| 31 | +Add the `@sentry/react-native/metro` plugin to your Metro configuration and enable the `reactComponentAnnotation` option: |
| 32 | + |
| 33 | +```javascript {tabTitle:React Native} {filename:metro.config.js} |
| 34 | +const { getDefaultConfig } = require("@react-native/metro-config"); |
| 35 | +const { withSentryConfig } = require('@sentry/react-native/metro'); |
| 36 | + |
| 37 | +const config = getDefaultConfig(__dirname); |
| 38 | +module.exports = withSentryConfig(config, { annotateReactComponents: true }); |
| 39 | +``` |
| 40 | + |
| 41 | +```javascript {tabTitle:Expo} {filename:metro.config.js} |
| 42 | +// const { getDefaultConfig } = require("expo/metro-config"); |
| 43 | +const { getSentryExpoConfig } = require("@sentry/react-native/metro"); |
| 44 | + |
| 45 | +// const config = getDefaultConfig(__dirname); |
| 46 | +const config = getSentryExpoConfig(config, { annotateReactComponents: true }); |
| 47 | +``` |
| 48 | + |
| 49 | +## How It Works |
| 50 | + |
| 51 | +The Sentry React Native Metro plugin applies [`@sentry/babel-plugin-component-annotate`](https://www.npmjs.com/package/@sentry/babel-plugin-component-annotate), which parses your application's JSX source code at build time and adds additional `data` attributes to it. These attributes then appear on the nodes of your application's build, indicating the React component name and file that each node is sourced from. |
| 52 | + |
| 53 | +Here's an example of a component named `MyAwesomeComponent` in the file `myAwesomeComponent.jsx`: |
| 54 | + |
| 55 | +```javascript |
| 56 | +function MyAwesomeComponent() { |
| 57 | + return <Text>This is a really cool and awesome component!</Text> |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +Here's what the resulting node would look like if your bundler had applied the plugin and built your project: |
| 62 | + |
| 63 | +```html |
| 64 | +<Text |
| 65 | + data-sentry-component="MyAwesomeComponent" |
| 66 | + data-sentry-source-file="myAwesomeComponent.jsx" |
| 67 | +> |
| 68 | + This is a really cool and awesome component! |
| 69 | +</Text> |
| 70 | +``` |
| 71 | + |
| 72 | +The Sentry browser SDK will pick off the value from these `data` attributes and collect them when your components are interacted with. |
| 73 | + |
| 74 | +## Next Steps: |
| 75 | + |
| 76 | +- Lear more about Sentry's React Native [Metro bundler plugin](/platforms/react-native/manual-setup/metro/). |
0 commit comments