Skip to content
76 changes: 76 additions & 0 deletions docs/platforms/react-native/integrations/component-names.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
title: React Component Names
excerpt: ""
description: "Learn how Sentry's React Native SDK allows you to monitor your components."
sidebar_order: 57
---

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:


```
View > Touchable > View > Text
```

You can see exactly which React component was used, as in the example below:

```html
MyCard (View, MyCard.ts) > MyButton (Touchable, MyCard.ts) > View > Text
```
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.

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.

## Prerequisites

- [Install](/platforms/react-native/) React Native SDK `5.25.0-alpha.3` or newer.
- Ensure the React components you want to capture are in `.jsx` or `.tsx` file formats.

## Enable Component Name Capturing

Add the `@sentry/react-native/metro` plugin to your Metro configuration and enable the `reactComponentAnnotation` option:

```javascript {tabTitle:React Native} {filename:metro.config.js}
const { getDefaultConfig } = require("@react-native/metro-config");
const { withSentryConfig } = require('@sentry/react-native/metro');

const config = getDefaultConfig(__dirname);
module.exports = withSentryConfig(config, { annotateReactComponents: true });
```

```javascript {tabTitle:Expo} {filename:metro.config.js}
// const { getDefaultConfig } = require("expo/metro-config");
const { getSentryExpoConfig } = require("@sentry/react-native/metro");

// const config = getDefaultConfig(__dirname);
const config = getSentryExpoConfig(config, { annotateReactComponents: true });
```

## How It Works

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.

Here's an example of a component named `MyAwesomeComponent` in the file `myAwesomeComponent.jsx`:

```javascript
function MyAwesomeComponent() {
return <Text>This is a really cool and awesome component!</Text>
}
```

Here's what the resulting node would look like if your bundler had applied the plugin and built your project:

```html
<Text
data-sentry-component="MyAwesomeComponent"
data-sentry-source-file="myAwesomeComponent.jsx"
>
This is a really cool and awesome component!
</Text>
```

The Sentry browser SDK will pick off the value from these `data` attributes and collect them when your components are interacted with.

## Next Steps:

- Lear more about Sentry's React Native [Metro bundler plugin](/platforms/react-native/manual-setup/metro/).