|
| 1 | + |
| 2 | +**React** |
| 3 | + |
| 4 | +To get started with performance monitoring using Sentry's React SDK, first install the `@sentry/react` and `@sentry/apm` packages: |
| 5 | + |
| 6 | +```bash |
| 7 | +# Using yarn |
| 8 | +$ yarn add @sentry/react @sentry/apm |
| 9 | + |
| 10 | +# Using npm |
| 11 | +$ npm install @sentry/react @sentry/apm |
| 12 | +``` |
| 13 | + |
| 14 | +Next, initialize the integration in your call to `Sentry.init`. Make sure this happens before you mount your React component on the page. |
| 15 | + |
| 16 | +```jsx |
| 17 | +import * as Sentry from '@sentry/react'; |
| 18 | +import { Integrations } from "@sentry/apm"; |
| 19 | +Sentry.init({ |
| 20 | + dsn: '"___PUBLIC_DSN___"', |
| 21 | + release: 'my-project-name@' + process.env.npm_package_version, |
| 22 | + integrations: [ |
| 23 | + new Integrations.Tracing(), |
| 24 | + ], |
| 25 | + tracesSampleRate: 0.25, // must be present and non-zero |
| 26 | +}); |
| 27 | + |
| 28 | +// ... |
| 29 | + |
| 30 | +ReactDOM.render(<App />, rootNode); |
| 31 | + |
| 32 | +// Can also use with React Concurrent Mode |
| 33 | +// ReactDOM.createRoot(rootNode).render(<App />); |
| 34 | +``` |
| 35 | + |
| 36 | +**`withProfiler` Higher-Order Component** |
| 37 | + |
| 38 | +`@sentry/react` exports a `withProfiler` higher-order component that leverages the `@sentry/apm` Tracing integration to add React related spans to transactions. If the Tracing integration is not enabled, the Profiler will not work. |
| 39 | + |
| 40 | +In the example below, the `withProfiler` higher-order component is used to instrument an App component. |
| 41 | + |
| 42 | +```jsx |
| 43 | +import React from 'react'; |
| 44 | +import * as Sentry from '@sentry/react'; |
| 45 | + |
| 46 | +class App extends React.Component { |
| 47 | + render() { |
| 48 | + return ( |
| 49 | + <FancyComponent> |
| 50 | + <NestedComponent someProp={2} /> |
| 51 | + <AnotherComponent /> |
| 52 | + </FancyComponent> |
| 53 | + ) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +export default Sentry.withProfiler(App); |
| 58 | +``` |
| 59 | + |
| 60 | +The React Profiler currently generates spans with three different kinds of op-codes: `react.mount`, `react.render`, and `react.update`. |
| 61 | + |
| 62 | +`react.mount` |
| 63 | + |
| 64 | +: The span that represents how long it took for the profiled component to mount. |
| 65 | + |
| 66 | +`react.render` |
| 67 | + |
| 68 | +: The span that represents how long the profiled component was on a page. This span is only generated if the profiled component mounts and unmounts while a transaction is occurring. |
| 69 | + |
| 70 | +`react.update` |
| 71 | + |
| 72 | +: The span that represents when the profiled component updated. This span is only generated if the profiled component has mounted. |
| 73 | + |
| 74 | +{% capture __alert_content -%} |
| 75 | +In [React Strict Mode](https://reactjs.org/docs/strict-mode.html), certain component methods will be [invoked twice](https://reactjs.org/docs/strict-mode.html#detecting-unexpected-side-effects). This may lead to duplicate `react.mount` spans appearing in a transaction. React Strict Mode only runs in development mode, so this will have no impact on your production traces. |
| 76 | +{%- endcapture -%} |
| 77 | +{%- include components/alert.html |
| 78 | + title="Note" |
| 79 | + content=__alert_content |
| 80 | + level="warning" |
| 81 | +%} |
| 82 | + |
| 83 | +The `withProfiler` higher-order component has a variety of options for further customization. They can be passed in as the second argument to the `withProfiler` function. |
| 84 | + |
| 85 | +```jsx |
| 86 | +export default Sentry.withProfiler(App, { name: "CustomAppName" }) |
| 87 | +``` |
| 88 | + |
| 89 | +`name` (string) |
| 90 | + |
| 91 | +: The name of the component being profiled. By default, the name is taken from the component `displayName` property or the component `name` property. |
| 92 | + |
| 93 | +`includeRender` (boolean) |
| 94 | + |
| 95 | +: If a `react.render` span should be created by the Profiler. Set as true by default. |
| 96 | + |
| 97 | +`includeUpdates` (boolean) |
| 98 | + |
| 99 | +: If `react.update` spans should be created by the Profiler. Set as true by default. We recommend setting this prop as false for components that will experience many rerenders, such as text input components, as the resulting spans can be very noisy. |
0 commit comments