Skip to content

Commit 64a3247

Browse files
onurtemizkanLuca Forstner
andauthored
Add React SDK's wrapCreateMemoryRouterVX docs. (#12309)
Co-authored-by: Luca Forstner <[email protected]>
1 parent 9128583 commit 64a3247

File tree

2 files changed

+34
-40
lines changed
  • docs/platforms/javascript/guides/react/features/react-router

2 files changed

+34
-40
lines changed

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ sidebar_order: 20
55
---
66

77
<Alert level="info">
8-
- React Router v6 support is included in the `@sentry/react` package since version `7`.
8+
- React Router v6 support is included in the `@sentry/react` package since
9+
version `7`.
910
</Alert>
1011

1112
Update your `Sentry.browserTracingIntegration` to `Sentry.reactRouterV6BrowserTracingIntegration` and provide the required React hooks and router functions:
@@ -17,20 +18,24 @@ Update your `Sentry.browserTracingIntegration` to `Sentry.reactRouterV6BrowserTr
1718
<Alert level="warning">
1819

1920
To ensure proper routing instrumentation, initialize Sentry by calling `Sentry.init` **before**:
21+
2022
- Wrapping your `<Routes />` component
2123
- Using `useRoutes`
22-
- Using `createBrowserRouterV6`
24+
- Using `wrapCreateBrowserRouterV6` or `wrapCreateMemoryRouterV6`
2325

2426
</Alert>
2527

26-
### Usage with `createBrowserRouter`
28+
### Usage with `createBrowserRouter` or `createMemoryRouter`
2729

28-
<Alert level="info">
29-
Available in `@sentry/react` version `7.21.0` and above.
30-
</Alert>
30+
If you choose to create your router instance with [`createBrowserRouter`](https://reactrouter.com/en/main/routers/create-browser-router) or [`createMemoryRouter`](https://reactrouter.com/en/main/routers/create-memory-router), you can use `Sentry.wrapCreateBrowserRouterV6` or `Sentry.wrapCreateMemoryRouterV6` to wrap it with the instrumentation:
31+
32+
<Alert level="warning" title="Note">
3133

32-
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:
34+
`wrapCreateMemoryRouterV6` was introduced in SDK version 8.50.0. Prior to that version, we suggested using `wrapCreateBrowserRouterV6` with `createMemoryRouter`. If you are currently using `wrapCreateBrowserRouterV6` to wrap `createMemoryRouter`, it is recommended that you use `wrapCreateMemoryRouterV6` instead.
3335

36+
You can instrument [`createHashRouter`](https://reactrouter.com/en/main/routers/create-hash-router) using the `wrapCreateBrowserRouterV6` function.
37+
38+
</Alert>
3439

3540
```javascript {2-8, 15-21, 26-33}
3641
import React from "react";
@@ -66,18 +71,10 @@ const router = sentryCreateBrowserRouter([
6671
]);
6772
```
6873

69-
<Alert level="warning" title="Note">
70-
71-
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 `wrapCreateBrowserRouterV6` function.
72-
73-
</Alert>
74-
75-
7674
### Usage With `<Routes />` Component
7775

7876
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:
7977

80-
8178
```javascript {3-11, 18-24, 29, 33-35}
8279
import React from "react";
8380
import ReactDOM from "react-dom";
@@ -123,7 +120,7 @@ This is only needed at the top level of your app, rather than how v4/v5 required
123120
### Usage With `useRoutes` Hook
124121

125122
<Alert level="info">
126-
Available in `@sentry/react` version `7.12.1` and above.
123+
Available in `@sentry/react` version `7.12.1` and above.
127124
</Alert>
128125

129126
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.
@@ -134,7 +131,6 @@ If you specify your route definitions as an object to the [`useRoutes` hook](htt
134131

135132
</Alert>
136133

137-
138134
```javascript {2-10, 15-21, 26, 29-31}
139135
import React from "react";
140136
import {
@@ -179,13 +175,13 @@ ReactDOM.render(
179175

180176
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.
181177

182-
183178
### Custom Error Boundaries
184179

185180
When using `react-router`, errors thrown inside route elements will only be re-thrown in **development mode** while using [`strict mode`](https://react.dev/reference/react/StrictMode). In production, these errors won't be surfaced unless manually captured. If you **don't** have a custom error boundary in place, `react-router` will create a default one that "swallows" all errors.
186181

187182
<Note>
188-
Note, that this only applies to render method and lifecycle errors since React doesn't need error boundaries to handle errors in event handlers.
183+
Note, that this only applies to render method and lifecycle errors since React
184+
doesn't need error boundaries to handle errors in event handlers.
189185
</Note>
190186

191187
To send errors to Sentry while using a custom error boundary, use the `Sentry.captureException` method:
@@ -203,7 +199,7 @@ const router = sentryCreateBrowserRouter([
203199
element: <Outlet />,
204200
errorElement: <YourCustomRootErrorBoundary />,
205201
children: [
206-
// other routes ...
202+
// other routes ...
207203
],
208204
},
209205
],
@@ -230,7 +226,6 @@ export function YourCustomRootErrorBoundary() {
230226

231227
```
232228

233-
234229
## Next Steps:
235230

236231
- [Return to **Getting Started**](../../)

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

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ sidebar_order: 10
55
---
66

77
<Alert level="info">
8-
- React Router v7 (library mode) support is included in the `@sentry/react` package since version `8.42.0`.
9-
- React Router v7 (framework mode) is not yet supported.
8+
- React Router v7 (library mode) support is included in the `@sentry/react`
9+
package since version `8.42.0`. - React Router v7 (framework mode) is not yet
10+
supported.
1011
</Alert>
1112

1213
Update your `Sentry.browserTracingIntegration` to `Sentry.reactRouterV7BrowserTracingIntegration` and provide the required React hooks and router functions:
@@ -18,16 +19,24 @@ Update your `Sentry.browserTracingIntegration` to `Sentry.reactRouterV7BrowserTr
1819
<Alert level="warning">
1920

2021
To ensure proper routing instrumentation, initialize Sentry by calling `Sentry.init` **before**:
22+
2123
- Wrapping your `<Routes />` component
2224
- Using `useRoutes`
23-
- Using `createBrowserRouterV7`
25+
- Using `wrapCreateBrowserRouterV7` or `wrapCreateMemoryRouterV7`
2426

2527
</Alert>
2628

27-
### Usage with `createBrowserRouter`
29+
### Usage with `createBrowserRouter` or `createMemoryRouter`
30+
31+
If you choose to create your router instance with [`createBrowserRouter`](https://reactrouter.com/en/main/routers/create-browser-router) or [`createMemoryRouter`](https://reactrouter.com/en/main/routers/create-memory-router), you can use `Sentry.wrapCreateBrowserRouterV7` or `Sentry.wrapCreateMemoryRouterV7` to wrap it with the instrumentation:
32+
33+
<Alert level="warning" title="Note">
34+
35+
`wrapCreateMemoryRouterV7` was introduced in SDK version 8.50.0. Prior to that version, we suggested using `wrapCreateBrowserRouterV7` with `createMemoryRouter`. If you are currently using `wrapCreateBrowserRouterV7` to wrap `createMemoryRouter`, it is recommended that you use `wrapCreateMemoryRouterV7` instead.
2836

29-
If you choose to create your router instance with [`createBrowserRouter`](https://reactrouter.com/en/main/routers/create-browser-router) from the `react-router` package, you can use `Sentry.wrapCreateBrowserRouterV7` to wrap it with the instrumentation:
37+
You can instrument [`createHashRouter`](https://reactrouter.com/en/main/routers/create-hash-router) using the `wrapCreateBrowserRouterV7` function.
3038

39+
</Alert>
3140

3241
```javascript {2-8, 15-21, 26-33}
3342
import React from "react";
@@ -63,18 +72,10 @@ const router = sentryCreateBrowserRouter([
6372
]);
6473
```
6574

66-
<Alert level="warning" title="Note">
67-
68-
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.
69-
70-
</Alert>
71-
72-
7375
### Usage With `<Routes />` Component
7476

7577
If you're using the `<Routes />` component to define your routes, wrap [`Routes`](https://reactrouter.com/en/main/components/routes) using `Sentry.withSentryReactRouterV7Routing`. This creates a higher order component, which will enable Sentry to reach your router context. You can also use `Sentry.withSentryReactRouterV7Routing` for `Routes` inside `BrowserRouter`. `MemoryRouter`, and `HashRouter` components:
7678

77-
7879
```javascript {3-11, 18-24, 29, 33-35}
7980
import React from "react";
8081
import ReactDOM from "react-dom";
@@ -127,7 +128,6 @@ If you specify your route definitions as an object to the [`useRoutes` hook](htt
127128

128129
</Alert>
129130

130-
131131
```javascript {2-10, 15-21, 26, 29-31}
132132
import React from "react";
133133
import {
@@ -172,18 +172,17 @@ ReactDOM.render(
172172

173173
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.
174174

175-
176175
### Custom Error Boundaries
177176

178177
When using `react-router`, errors thrown inside route elements will only be re-thrown in **development mode** while using [`strict mode`](https://react.dev/reference/react/StrictMode). In production, these errors won't be surfaced unless manually captured. If you **don't** have a custom error boundary in place, `react-router` will create a default one that "swallows" all errors.
179178

180179
<Note>
181-
Note, that this only applies to render method and lifecycle errors since React doesn't need error boundaries to handle errors in event handlers.
180+
Note, that this only applies to render method and lifecycle errors since React
181+
doesn't need error boundaries to handle errors in event handlers.
182182
</Note>
183183

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

186-
187186
```jsx {11, 28}
188187
// router setup
189188
const sentryCreateBrowserRouter = wrapCreateBrowserRouterV7(createBrowserRouter);
@@ -197,7 +196,7 @@ const router = sentryCreateBrowserRouter([
197196
element: <Outlet />,
198197
errorElement: <YourCustomRootErrorBoundary />,
199198
children: [
200-
// other routes ...
199+
// other routes ...
201200
],
202201
},
203202
],
@@ -227,4 +226,4 @@ export function YourCustomRootErrorBoundary() {
227226
## Next Steps:
228227

229228
- [Return to **Getting Started**](../../)
230-
- [Return to the main integrations page](../)
229+
- [Return to the main integrations page](../)

0 commit comments

Comments
 (0)