Skip to content

Commit 0519ad5

Browse files
Add React Router v6 instrumentation docs. (#5037)
Co-authored-by: Isabel <[email protected]>
1 parent e046ba3 commit 0519ad5

File tree

1 file changed

+57
-1
lines changed
  • src/platforms/javascript/guides/react/configuration/integrations

1 file changed

+57
-1
lines changed

src/platforms/javascript/guides/react/configuration/integrations/react-router.mdx

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,63 @@ The React Router integration is designed to work with our Tracing SDK, `@sentry/
1515

1616
</Alert>
1717

18-
We support integrations for React Router 3, 4 and 5.
18+
We support integrations for React Router 3, 4, 5, and 6.
19+
20+
## React Router v6
21+
_(Available in version 7 and above)_
22+
23+
To use React Router v6 with Sentry:
24+
25+
1. Initialize `Sentry.reactRouterV6Instrumentation` as your routing instrumentation and provide the functions it needs to enable performance tracing:
26+
27+
- `useEffect` hook from `react`
28+
- `useLocation` and `useNavigationType` hooks from `react-router-dom`
29+
- `createRoutesFromChildren` and `matchRoutes` functions from `react-router-dom` or `react-router` packages.
30+
31+
2. Wrap [`Routes`](https://reactrouter.com/docs/en/v6/api#routes-and-route) using `Sentry.withSentryReactRouterV6Routing` to create a higher order component, which will enable Sentry to reach your router context, as in the example below:
32+
33+
```javascript
34+
import React from 'react';
35+
import ReactDOM from "react-dom";
36+
import {
37+
Routes,
38+
BrowserRouter,
39+
useLocation,
40+
useNavigationType,
41+
createRoutesFromChildren,
42+
matchRoutes,
43+
} from "react-router-dom";
44+
import * as Sentry from "@sentry/react";
45+
import { BrowserTracing } from "@sentry/tracing";
46+
47+
Sentry.init({
48+
integrations: [
49+
new BrowserTracing({
50+
routingInstrumentation: Sentry.reactRouterV6Instrumentation(
51+
React.useEffect,
52+
useLocation,
53+
useNavigationType,
54+
createRoutesFromChildren,
55+
matchRoutes,
56+
),
57+
}),
58+
],
59+
tracesSampleRate: 1.0,
60+
});
61+
62+
const SentryRoutes = Sentry.withSentryReactRouterV6Routing(Routes)
63+
64+
ReactDOM.render(
65+
<BrowserRouter>
66+
<SentryRoutes>
67+
<Route path="/" element={<div>Home</div>} />
68+
</SentryRoutes>
69+
</BrowserRouter>,
70+
);
71+
```
72+
73+
Now, Sentry should generate `pageload`/`navigation` transactions with parameterized transaction names (for example, /teams/:teamid/user/:userid), where applicable.
74+
1975

2076
## React Router v4/v5
2177

0 commit comments

Comments
 (0)