Skip to content

Commit 763529c

Browse files
Lms24s1gr1d
andauthored
ref(react): Improve React Router instrumentation docs (#10837)
Add some highlighting to the various RR version code snippets and slightly streamlines wording --------- Co-authored-by: Sigrid Huemer <[email protected]>
1 parent bbabbbf commit 763529c

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

docs/platforms/javascript/guides/react/features/react-router.mdx

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ _(Available in version 7 and above)_
2323

2424
To use React Router v6 with Sentry:
2525

26-
Add `Sentry.reactRouterV6BrowserTracingIntegration` instead of the regular `Sentry.browserTracingIntegration` and provide the functions it needs to enable performance tracing:
26+
Add `Sentry.reactRouterV6BrowserTracingIntegration` instead of the regular `Sentry.browserTracingIntegration` and provide the required React hooks and router functions:
2727

2828
- `useEffect` hook from `react`
2929
- `useLocation` and `useNavigationType` hooks from `react-router-dom`
30-
- `createRoutesFromChildren` and `matchRoutes` functions from `react-router-dom` or `react-router` packages.
30+
- `createRoutesFromChildren` and `matchRoutes` functions from `react-router-dom` or `react-router` packages, depending on which package you're using.
3131

3232
<Alert level="warning">
3333

34-
Make sure `Sentry.reactRouterV6BrowserTracingIntegration` is initialized by your `Sentry.init` call, before you wrap `<Routes />` component or `useRoutes` hook. Otherwise, the routing instrumentation may not work properly.
34+
Make sure you call `Sentry.init`, **before** you wrap your `<Routes />` component or the `useRoutes` hook. Otherwise, the routing instrumentation may not work properly.
3535

3636
</Alert>
3737

@@ -43,8 +43,16 @@ If you choose to create your router instance with [`createBrowserRouter`](https:
4343

4444
<SignInNote />
4545

46-
```javascript
47-
import { createBrowserRouter } from "react-router-dom";
46+
```javascript {2-8, 15-21, 26-33}
47+
import React from "react";
48+
import {
49+
createBrowserRouter,
50+
createRoutesFromChildren,
51+
matchRoutes,
52+
useLocation,
53+
useNavigationType,
54+
} from "react-router-dom";
55+
4856
import * as Sentry from "@sentry/react";
4957

5058
Sentry.init({
@@ -65,7 +73,7 @@ const sentryCreateBrowserRouter =
6573
Sentry.wrapCreateBrowserRouter(createBrowserRouter);
6674

6775
const router = sentryCreateBrowserRouter([
68-
// ...
76+
// your routes...
6977
]);
7078
```
7179

@@ -81,7 +89,7 @@ If you're using the `<Routes />` component from `react-router-dom` to define you
8189

8290
<SignInNote />
8391

84-
```javascript
92+
```javascript {3-11, 18-24, 29, 33-35}
8593
import React from "react";
8694
import ReactDOM from "react-dom";
8795
import {
@@ -93,6 +101,7 @@ import {
93101
createRoutesFromChildren,
94102
matchRoutes,
95103
} from "react-router-dom";
104+
96105
import * as Sentry from "@sentry/react";
97106

98107
Sentry.init({
@@ -136,22 +145,23 @@ If you specify your route definitions as an object to the [`useRoutes` hook](htt
136145

137146
<SignInNote />
138147

139-
```javascript
148+
```javascript {2-10, 15-21, 26, 29-31}
149+
import React from "react";
140150
import {
141151
createRoutesFromChildren,
142152
matchRoutes,
143153
useLocation,
144154
useNavigationType,
145155
useRoutes,
146156
} from "react-router-dom";
157+
147158
import { wrapUseRoutes } from "@sentry/react";
148-
import { useEffect } from "react";
149159

150160
Sentry.init({
151161
dsn: "___PUBLIC_DSN___",
152162
integrations: [
153163
Sentry.reactRouterV6BrowserTracingIntegration({
154-
useEffect,
164+
useEffect: React.useEffect,
155165
useLocation,
156166
useNavigationType,
157167
createRoutesFromChildren,
@@ -165,7 +175,7 @@ const useSentryRoutes = wrapUseRoutes(useRoutes);
165175

166176
function App() {
167177
return useSentryRoutes([
168-
// ...
178+
// your routes...
169179
]);
170180
}
171181

@@ -191,7 +201,7 @@ We recommend you use the `withSentryRouting` higher order component to create a
191201

192202
<SignInNote />
193203

194-
```javascript
204+
```javascript {1, 7, 14-16, 28-30}
195205
import {Route, Router, Switch } from 'react-router-dom';
196206
import { createBrowserHistory } from 'history';
197207

@@ -206,7 +216,7 @@ Sentry.init({
206216
dsn: "___PUBLIC_DSN___",
207217
integrations: [
208218
Sentry.reactRouterV5BrowserTracingIntegration({ history }),
209-
// or
219+
// OR
210220
Sentry.reactRouterV4BrowserTracingIntegration({ history }),
211221
],
212222

@@ -232,7 +242,7 @@ If you don't want to wrap individual routes, you can still specify parameterized
232242

233243
<SignInNote />
234244

235-
```javascript
245+
```javascript {1,8-10,15-19}
236246
import { Route, Router, Switch, matchPath } from 'react-router-dom';
237247
import { createBrowserHistory } from 'history';
238248

@@ -279,7 +289,7 @@ To use the router integration, import and set a custom routing instrumentation a
279289

280290
<SignInNote />
281291

282-
```javascript
292+
```javascript {18-23}
283293
import * as Router from "react-router";
284294

285295
import * as Sentry from "@sentry/react";

0 commit comments

Comments
 (0)