Skip to content

Commit 5420697

Browse files
committed
split v6 and v7
1 parent 213e298 commit 5420697

File tree

2 files changed

+240
-170
lines changed
  • docs/platforms/javascript/guides/react/features/react-router

2 files changed

+240
-170
lines changed

docs/platforms/javascript/guides/react/features/react-router/v6-7.mdx renamed to docs/platforms/javascript/guides/react/features/react-router/v6.mdx

Lines changed: 13 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
---
2-
title: React Router v6 and v7
3-
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."
44
sidebar_order: 20
55
---
66

77
<Alert level="info">
88
- 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.
119
</Alert>
1210

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:
1412

1513
- `useEffect` hook from `react`
1614
- `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
2523
### Usage with `createBrowserRouter`
2624

2725
<Alert level="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.
2927
</Alert>
3028

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:
3230

3331

34-
```javascript {2-8, 15-21, 26-33} {tabTitle: Version 6}
32+
```javascript {2-8, 15-21, 26-33}
3533
import React from "react";
3634
import {
3735
createBrowserRouter,
@@ -65,40 +63,6 @@ const router = sentryCreateBrowserRouter([
6563
]);
6664
```
6765

68-
```javascript {2-8, 15-21, 26-33} {tabTitle: Version 7}
69-
import React from "react";
70-
import {
71-
createBrowserRouter,
72-
createRoutesFromChildren,
73-
matchRoutes,
74-
useLocation,
75-
useNavigationType,
76-
} from "react-router";
77-
78-
import * as Sentry from "@sentry/react";
79-
80-
Sentry.init({
81-
dsn: "___PUBLIC_DSN___",
82-
integrations: [
83-
Sentry.reactRouterV7BrowserTracingIntegration({
84-
useEffect: React.useEffect,
85-
useLocation,
86-
useNavigationType,
87-
createRoutesFromChildren,
88-
matchRoutes,
89-
}),
90-
],
91-
tracesSampleRate: 1.0,
92-
});
93-
94-
const sentryCreateBrowserRouter =
95-
Sentry.wrapCreateBrowserRouterV7(createBrowserRouter);
96-
97-
const router = sentryCreateBrowserRouter([
98-
// your routes...
99-
]);
100-
```
101-
10266
<Alert level="warning" title="Note">
10367

10468
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
10872

10973
### Usage With `<Routes />` Component
11074

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:
11276

11377

114-
```javascript {3-11, 18-24, 29, 33-35} {tabTitle: Version 6}
78+
```javascript {3-11, 18-24, 29, 33-35}
11579
import React from "react";
11680
import ReactDOM from "react-dom";
11781
import {
@@ -151,64 +115,24 @@ ReactDOM.render(
151115
);
152116
```
153117

154-
```javascript {3-11, 18-24, 29, 33-35} {tabTitle: Version 7}
155-
import React from "react";
156-
import ReactDOM from "react-dom";
157-
import {
158-
Routes,
159-
Route,
160-
BrowserRouter,
161-
useLocation,
162-
useNavigationType,
163-
createRoutesFromChildren,
164-
matchRoutes,
165-
} from "react-router";
166-
167-
import * as Sentry from "@sentry/react";
168-
169-
Sentry.init({
170-
dsn: "___PUBLIC_DSN___",
171-
integrations: [
172-
Sentry.reactRouterV7BrowserTracingIntegration({
173-
useEffect: React.useEffect,
174-
useLocation,
175-
useNavigationType,
176-
createRoutesFromChildren,
177-
matchRoutes,
178-
}),
179-
],
180-
tracesSampleRate: 1.0,
181-
});
182-
183-
const SentryRoutes = Sentry.withSentryReactRouterV7Routing(Routes);
184-
185-
ReactDOM.render(
186-
<BrowserRouter>
187-
<SentryRoutes>
188-
<Route path="/" element={<div>Home</div>} />
189-
</SentryRoutes>
190-
</BrowserRouter>
191-
);
192-
```
193-
194118
This is only needed at the top level of your app, rather than how v4/v5 required wrapping every `<Route/>` you wanted parametrized.
195119

196120
### Usage With `useRoutes` Hook
197121

198122
<Alert level="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.
200124
</Alert>
201125

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.
203127

204128
<Alert level="warning">
205129

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).
207131

208132
</Alert>
209133

210134

211-
```javascript {2-10, 15-21, 26, 29-31} {tabTitle: Version 6}
135+
```javascript {2-10, 15-21, 26, 29-31}
212136
import React from "react";
213137
import {
214138
createRoutesFromChildren,
@@ -250,48 +174,6 @@ ReactDOM.render(
250174
);
251175
```
252176

253-
```javascript {2-10, 15-21, 26, 29-31} {tabTitle: Version 7}
254-
import React from "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-
const useSentryRoutes = wrapUseRoutesV7(useRoutes);
280-
281-
function App() {
282-
return useSentryRoutes([
283-
// your routes...
284-
]);
285-
}
286-
287-
ReactDOM.render(
288-
<BrowserRouter>
289-
<App />
290-
</BrowserRouter>,
291-
document.getElementById("root")
292-
);
293-
```
294-
295177
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.
296178

297179

@@ -305,7 +187,7 @@ Note, that this only applies to render method and lifecycle errors since React d
305187

306188
To send errors to Sentry while using a custom error boundary, use the `Sentry.captureException` method:
307189

308-
```jsx {11, 28} {tabTitle: Version 6}
190+
```jsx {11, 28}
309191
// router setup
310192
const sentryCreateBrowserRouter = wrapCreateBrowserRouterV6(createBrowserRouter);
311193
const router = sentryCreateBrowserRouter([
@@ -345,45 +227,6 @@ export function YourCustomRootErrorBoundary() {
345227

346228
```
347229

348-
```jsx {11, 28} {tabTitle: Version 7}
349-
// router setup
350-
const sentryCreateBrowserRouter = wrapCreateBrowserRouterV7(createBrowserRouter);
351-
const router = sentryCreateBrowserRouter([
352-
{
353-
path: "/",
354-
element: <YourLayout />,
355-
children: [
356-
{
357-
path: "",
358-
element: <Outlet />,
359-
errorElement: <YourCustomRootErrorBoundary />,
360-
children: [
361-
// other routes ...
362-
],
363-
},
364-
],
365-
},
366-
]);
367-
368-
// error boundary
369-
import { useRouteError } from "react-router-dom";
370-
import * as Sentry from "@sentry/react";
371-
372-
export function YourCustomRootErrorBoundary() {
373-
const error = useRouteError() as Error;
374-
375-
React.useEffect(() => {
376-
Sentry.captureException(error);
377-
}, [error]);
378-
379-
return (
380-
<div>
381-
<h1>Ouch!</h1>
382-
</div>
383-
);
384-
}
385-
386-
```
387230

388231
## Next Steps:
389232

0 commit comments

Comments
 (0)