|
7 | 7 |
|
8 | 8 | # Official Sentry SDK for ReactJS
|
9 | 9 |
|
10 |
| -Note this library is in active development and not ready for production usage. |
11 |
| - |
12 | 10 | ## Links
|
13 | 11 |
|
14 | 12 | - [Official SDK Docs](https://docs.sentry.io/quickstart/)
|
15 | 13 | - [TypeDoc](http://getsentry.github.io/sentry-javascript/)
|
| 14 | + |
| 15 | +## General |
| 16 | + |
| 17 | +This package is a wrapper around `@sentry/browser`, with added functionality related to React. All methods available in |
| 18 | +`@sentry/browser` can be imported from `@sentry/react`. |
| 19 | + |
| 20 | +To use this SDK, call `Sentry.init(options)` before you mount your React component. |
| 21 | + |
| 22 | +```javascript |
| 23 | +import React from 'react'; |
| 24 | +import ReactDOM from "react-dom"; |
| 25 | +import * as Sentry from '@sentry/react'; |
| 26 | + |
| 27 | +Sentry.init({ |
| 28 | + dsn: '__DSN__', |
| 29 | + // ... |
| 30 | +}); |
| 31 | + |
| 32 | +// ... |
| 33 | + |
| 34 | +ReactDOM.render(<App />, rootNode); |
| 35 | + |
| 36 | +// Can also use with React Concurrent Mode |
| 37 | +// ReactDOM.createRoot(rootNode).render(<App />); |
| 38 | +``` |
| 39 | + |
| 40 | +### ErrorBoundary |
| 41 | + |
| 42 | +`@sentry/react` exports an ErrorBoundary component that will automatically send Javascript errors from inside a |
| 43 | +component tree to Sentry, and set a fallback UI. Requires React version >= 16. |
| 44 | + |
| 45 | +> app.js |
| 46 | +```javascript |
| 47 | +import React from 'react'; |
| 48 | +import * as Sentry from '@sentry/react'; |
| 49 | + |
| 50 | +function FallbackComponent() { |
| 51 | + return ( |
| 52 | + <div>An error has occured</div> |
| 53 | + ) |
| 54 | +} |
| 55 | + |
| 56 | +class App extends React.Component { |
| 57 | + render() { |
| 58 | + return ( |
| 59 | + <Sentry.ErrorBoundary fallback={FallbackComponent} showDialog> |
| 60 | + <OtherComponents /> |
| 61 | + </Sentry.ErrorBoundary> |
| 62 | + ) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +export default App; |
| 67 | +``` |
| 68 | + |
| 69 | +### Profiler |
| 70 | + |
| 71 | +`@sentry/react` exports a Profiler component that leverages the `@sentry/apm` Tracing integration to add React related |
| 72 | +spans to transactions. If the Tracing integration is not enabled, the Profiler component will not work. The Profiler |
| 73 | +tracks component mount, render duration and updates. Requires React version >= 15. |
| 74 | + |
| 75 | +> app.js |
| 76 | +```javascript |
| 77 | +import React from 'react'; |
| 78 | +import * as Sentry from '@sentry/react'; |
| 79 | + |
| 80 | +class App extends React.Component { |
| 81 | + render() { |
| 82 | + return ( |
| 83 | + <FancyComponent> |
| 84 | + <InsideComponent someProp={2} /> |
| 85 | + <AnotherComponent /> |
| 86 | + </FancyComponent> |
| 87 | + ) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export default Sentry.withProfiler(App); |
| 92 | +``` |
0 commit comments