You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: "Learn about Sentry's React Router v6 and v7 integration."
2
+
title: React Router v6
3
+
description: "Learn about Sentry's React Router v6 integration."
4
4
sidebar_order: 20
5
5
---
6
6
7
7
<Alertlevel="info">
8
8
- React Router v6 support is included in the `@sentry/react` package since version `7`.
9
-
- React Router v7 support is included in the `@sentry/react` package since version `8.42.0`.
10
-
- React Router v7 in framework mode is not yet supported.
11
9
</Alert>
12
10
13
-
Update your `Sentry.browserTracingIntegration` to `Sentry.reactRouterV{6|7}BrowserTracingIntegration` and provide the required React hooks and router functions:
11
+
Update your `Sentry.browserTracingIntegration` to `Sentry.reactRouterV6BrowserTracingIntegration` and provide the required React hooks and router functions:
14
12
15
13
-`useEffect` hook from `react`
16
14
-`useLocation` and `useNavigationType` hooks from `react-router-dom` or `react-router`
@@ -25,13 +23,13 @@ Make sure you call `Sentry.init`, **before** you wrap your `<Routes />` componen
25
23
### Usage with `createBrowserRouter`
26
24
27
25
<Alertlevel="info">
28
-
For React Router v6 this is available in `@sentry/react` version `7.21.0` and above.
26
+
Available in `@sentry/react` version `7.21.0` and above.
29
27
</Alert>
30
28
31
-
If you choose to create your router instance with [`createBrowserRouter`](https://reactrouter.com/en/main/routers/create-browser-router) from the `react-router-dom` package, you can use `Sentry.wrapCreateBrowserRouterV{6|7}` to wrap it with the instrumentation:
29
+
If you choose to create your router instance with [`createBrowserRouter`](https://reactrouter.com/en/main/routers/create-browser-router) from the `react-router-dom` package, you can use `Sentry.wrapCreateBrowserRouterV6` to wrap it with the instrumentation:
32
30
33
31
34
-
```javascript {2-8, 15-21, 26-33} {tabTitle: Version 6}
You can instrument [`createMemoryRouter`](https://reactrouter.com/en/main/routers/create-memory-router) and [`createHashRouter`](https://reactrouter.com/en/main/routers/create-hash-router) using the `wrapCreateBrowserRouter` function.
@@ -108,10 +72,10 @@ You can instrument [`createMemoryRouter`](https://reactrouter.com/en/main/router
108
72
109
73
### Usage With `<Routes />` Component
110
74
111
-
If you're using the `<Routes />` component to define your routes, wrap [`Routes`](https://reactrouter.com/en/main/components/routes) using `Sentry.withSentryReactRouterV{6|7}Routing`. This creates a higher order component, which will enable Sentry to reach your router context. You can also use `Sentry.withSentryReactRouterV{6|7}Routing` for `Routes` inside `BrowserRouter`. `MemoryRouter`, and `HashRouter` components:
75
+
If you're using the `<Routes />` component to define your routes, wrap [`Routes`](https://reactrouter.com/en/main/components/routes) using `Sentry.withSentryReactRouterV6Routing`. This creates a higher order component, which will enable Sentry to reach your router context. You can also use `Sentry.withSentryReactRouterV6Routing` for `Routes` inside `BrowserRouter`. `MemoryRouter`, and `HashRouter` components:
112
76
113
77
114
-
```javascript {3-11, 18-24, 29, 33-35} {tabTitle: Version 6}
78
+
```javascript {3-11, 18-24, 29, 33-35}
115
79
importReactfrom"react";
116
80
importReactDOMfrom"react-dom";
117
81
import {
@@ -151,64 +115,24 @@ ReactDOM.render(
151
115
);
152
116
```
153
117
154
-
```javascript {3-11, 18-24, 29, 33-35} {tabTitle: Version 7}
This is only needed at the top level of your app, rather than how v4/v5 required wrapping every `<Route/>` you wanted parametrized.
195
119
196
120
### Usage With `useRoutes` Hook
197
121
198
122
<Alertlevel="info">
199
-
For React Router v6 this is available in `@sentry/react` version `7.12.1` and above.
123
+
Available in `@sentry/react` version `7.12.1` and above.
200
124
</Alert>
201
125
202
-
If you specify your route definitions as an object to the [`useRoutes` hook](https://reactrouter.com/en/main/hooks/use-routes), use `Sentry.wrapUseRoutesV{6|7}` to create a patched `useRoutes` hook that instruments your routes with Sentry.
126
+
If you specify your route definitions as an object to the [`useRoutes` hook](https://reactrouter.com/en/main/hooks/use-routes), use `Sentry.wrapUseRoutesV6` to create a patched `useRoutes` hook that instruments your routes with Sentry.
203
127
204
128
<Alertlevel="warning">
205
129
206
-
`wrapUseRoutesV{6|7}` should be called outside of a React component, as in the example below. It's also recommended that you assign the wrapped hook to a variable name starting with `use`, as per the [React documentation](https://reactjs.org/docs/hooks-custom.html#extracting-a-custom-hook).
130
+
`wrapUseRoutesV6` should be called outside of a React component, as in the example below. It's also recommended that you assign the wrapped hook to a variable name starting with `use`, as per the [React documentation](https://reactjs.org/docs/hooks-custom.html#extracting-a-custom-hook).
207
131
208
132
</Alert>
209
133
210
134
211
-
```javascript {2-10, 15-21, 26, 29-31} {tabTitle: Version 6}
135
+
```javascript {2-10, 15-21, 26, 29-31}
212
136
importReactfrom"react";
213
137
import {
214
138
createRoutesFromChildren,
@@ -250,48 +174,6 @@ ReactDOM.render(
250
174
);
251
175
```
252
176
253
-
```javascript {2-10, 15-21, 26, 29-31} {tabTitle: Version 7}
254
-
importReactfrom"react";
255
-
import {
256
-
createRoutesFromChildren,
257
-
matchRoutes,
258
-
useLocation,
259
-
useNavigationType,
260
-
useRoutes,
261
-
} from"react-router";
262
-
263
-
import { wrapUseRoutes } from"@sentry/react";
264
-
265
-
Sentry.init({
266
-
dsn:"___PUBLIC_DSN___",
267
-
integrations: [
268
-
Sentry.reactRouterV7BrowserTracingIntegration({
269
-
useEffect:React.useEffect,
270
-
useLocation,
271
-
useNavigationType,
272
-
createRoutesFromChildren,
273
-
matchRoutes,
274
-
}),
275
-
],
276
-
tracesSampleRate:1.0,
277
-
});
278
-
279
-
constuseSentryRoutes=wrapUseRoutesV7(useRoutes);
280
-
281
-
functionApp() {
282
-
returnuseSentryRoutes([
283
-
// your routes...
284
-
]);
285
-
}
286
-
287
-
ReactDOM.render(
288
-
<BrowserRouter>
289
-
<App />
290
-
</BrowserRouter>,
291
-
document.getElementById("root")
292
-
);
293
-
```
294
-
295
177
Now, Sentry should generate `pageload`/`navigation` transactions with parameterized transaction names (for example, `/teams/:teamid/user/:userid`), where applicable. This is only needed at the top level of your app, rather than how v4/v5 required wrapping every `<Route/>` you wanted parametrized.
296
178
297
179
@@ -305,7 +187,7 @@ Note, that this only applies to render method and lifecycle errors since React d
305
187
306
188
To send errors to Sentry while using a custom error boundary, use the `Sentry.captureException` method:
0 commit comments