Skip to content

Commit 78869b0

Browse files
docs(js): streamline React Router v6 and v7 documentation (#13138)
* streamline React Router v6 and v7 documentation * PR feedback * update custom error boundary section; add ToC * update error boundary content
1 parent aa1fd1f commit 78869b0

File tree

2 files changed

+46
-99
lines changed
  • docs/platforms/javascript/guides/react/features/react-router

2 files changed

+46
-99
lines changed

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

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,19 @@
11
---
22
title: React Router v6
3-
description: "Learn about Sentry's React Router v6 integration."
3+
description: "Learn how to instrument your React Router v6 application with Sentry."
44
sidebar_order: 20
55
---
66

7-
<Alert>
8-
- React Router v6 support is included in the `@sentry/react` package since
9-
version `7`.
10-
</Alert>
11-
12-
Update your `Sentry.browserTracingIntegration` to `Sentry.reactRouterV6BrowserTracingIntegration` and provide the required React hooks and router functions:
13-
14-
- `useEffect` hook from `react`
15-
- `useLocation` and `useNavigationType` hooks from `react-router-dom` or `react-router`
16-
- `createRoutesFromChildren` and `matchRoutes` functions from `react-router-dom` or `react-router`
17-
18-
<Alert level="warning">
19-
20-
To ensure proper routing instrumentation, initialize Sentry by calling `Sentry.init` **before**:
21-
22-
- Wrapping your `<Routes />` component
23-
- Using `useRoutes`
24-
- Using `wrapCreateBrowserRouterV6` or `wrapCreateMemoryRouterV6`
25-
26-
</Alert>
7+
Apply the following setup steps based on your routing method and create a [custom error boundary](#set-up-a-custom-error-boundary) to make sure Sentry automatically captures rendering errors:
278

28-
### Usage with `createBrowserRouter` or `createMemoryRouter`
9+
<TableOfContents ignoreIds={["set-up-a-custom-error-boundary", "next-steps"]} />
2910

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:
11+
## Usage with `createBrowserRouter` or `createMemoryRouter`
3112

32-
<Alert level="warning" title="Note">
13+
To instrument your React Router, update your `Sentry.browserTracingIntegration` to `Sentry.reactRouterV6BrowserTracingIntegration` within `Sentry.init` and provide the required React hooks and router functions. Then, wrap the router instance created by `createBrowserRouter` or `createMemoryRouter` with one of the following functions:
3314

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.
35-
36-
You can instrument [`createHashRouter`](https://reactrouter.com/en/main/routers/create-hash-router) using the `wrapCreateBrowserRouterV6` function.
37-
38-
</Alert>
15+
- Use `Sentry.wrapCreateBrowserRouterV6` for [`createBrowserRouter`](https://reactrouter.com/en/main/routers/create-browser-router) and [`createHashRouter`](https://reactrouter.com/en/main/routers/create-hash-router)
16+
- Use `Sentry.wrapCreateMemoryRouterV6` for [`createMemoryRouter`](https://reactrouter.com/en/main/routers/create-memory-router) (introduced in SDK version `8.50.0`)
3917

4018
```javascript {2-8, 15-21, 26-33}
4119
import React from "react";
@@ -71,9 +49,9 @@ const router = sentryCreateBrowserRouter([
7149
]);
7250
```
7351

74-
### Usage With `<Routes />` Component
52+
## Usage With `<Routes />` Component
7553

76-
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:
54+
If you're using the `<Routes />` component to define your routes, update your `Sentry.browserTracingIntegration` to `Sentry.reactRouterV6BrowserTracingIntegration` inside `Sentry.init` and provide the required React hooks and router functions. Then, wrap `<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.
7755

7856
```javascript {3-11, 18-24, 29, 33-35}
7957
import React from "react";
@@ -115,19 +93,16 @@ ReactDOM.render(
11593
);
11694
```
11795

118-
This is only needed at the top level of your app, rather than how v4/v5 required wrapping every `<Route/>` you wanted parametrized.
96+
This wrapper is only needed at the top level of your app, unlike React Router v4/v5, which required wrapping every `<Route />` you wanted parametrized.
11997

120-
### Usage With `useRoutes` Hook
98+
## Usage With `useRoutes` Hook
12199

122-
<Alert>
123-
Available in `@sentry/react` version `7.12.1` and above.
124-
</Alert>
100+
_Available in `@sentry/react` version `7.12.1` and above._
125101

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.
102+
If you specify your route definitions as an object to the [`useRoutes` hook](https://reactrouter.com/en/main/hooks/use-routes), update your `Sentry.browserTracingIntegration` to `Sentry.reactRouterV6BrowserTracingIntegration` inside `Sentry.init` and provide the required React hooks and router functions. Then, use `Sentry.wrapUseRoutesV6` to create a patched `useRoutes` hook that instruments your routes with Sentry.
127103

128-
<Alert level="warning">
129-
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).
104+
<Alert level="warning" title="Important">
105+
Call `wrapUseRoutesV6` outside of a React component, as in the example below. We also recommend that you assign the wrapped hook to a variable starting with `use`, as per [React's documentation](https://react.dev/learn/reusing-logic-with-custom-hooks#hook-names-always-start-with-use).
131106

132107
</Alert>
133108

@@ -173,16 +148,13 @@ ReactDOM.render(
173148
);
174149
```
175150

176-
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.
177-
178-
### Custom Error Boundaries
151+
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, unlike React Router v4/v5, which required wrapping every `<Route />` you wanted parametrized.
179152

180-
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.
153+
## Set Up a Custom Error Boundary
181154

182-
<Alert>
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.
185-
</Alert>
155+
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).\
156+
In production, these errors won't surface unless captured manually. If you **don't** have a custom error boundary in place, `react-router` will create a default one that "swallows" all errors.\
157+
Hence, to capture these errors with Sentry in production, we strongly recommend to implement a custom error boundary.
186158

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

@@ -226,7 +198,7 @@ export function YourCustomRootErrorBoundary() {
226198

227199
```
228200

229-
## Next Steps:
201+
## Next Steps
230202

231203
- [Return to **Getting Started**](../../)
232204
- [Return to the main integrations page](../)

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

Lines changed: 25 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,25 @@
11
---
22
title: React Router v7 (library mode)
3-
description: "Learn about Sentry's React Router v7 integration."
3+
description: "Learn how to instrument your React Router v7 application with Sentry."
44
sidebar_order: 10
55
---
66

7-
8-
<Alert level="warning" title="Looking for framework mode?">
9-
- React Router v7 (framework mode) is currently in experimental Alpha, check out the docs [here](/platforms/javascript/guides/react-router/).
10-
</Alert>
11-
12-
<Alert level="info">
13-
React Router v7 (library mode) support is included in the `@sentry/react`
14-
package since version `8.42.0`.
15-
</Alert>
16-
17-
Update your `Sentry.browserTracingIntegration` to `Sentry.reactRouterV7BrowserTracingIntegration` and provide the required React hooks and router functions:
18-
19-
- `useEffect` hook from `react`
20-
- `useLocation` and `useNavigationType` hooks from `react-router`
21-
- `createRoutesFromChildren` and `matchRoutes` functions from `react-router`
22-
23-
<Alert level="warning">
24-
25-
To ensure proper routing instrumentation, initialize Sentry by calling `Sentry.init` **before**:
26-
27-
- Wrapping your `<Routes />` component
28-
- Using `useRoutes`
29-
- Using `wrapCreateBrowserRouterV7` or `wrapCreateMemoryRouterV7`
30-
7+
<Alert level="info" title="Looking for framework mode?">
8+
React Router v7 (framework mode) is currently in experimental Alpha, check out
9+
the docs [here](/platforms/javascript/guides/react-router/).
3110
</Alert>
11+
Apply the following setup steps based on your routing method and create a
12+
[custom error boundary](#set-up-a-custom-error-boundary) to make sure Sentry
13+
automatically captures rendering errors:
3214

33-
### Usage with `createBrowserRouter` or `createMemoryRouter`
34-
35-
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:
36-
37-
<Alert level="warning" title="Note">
15+
<TableOfContents ignoreIds={["set-up-a-custom-error-boundary", "next-steps"]} />
3816

39-
`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.
17+
## Usage with `createBrowserRouter` or `createMemoryRouter`
4018

41-
You can instrument [`createHashRouter`](https://reactrouter.com/en/main/routers/create-hash-router) using the `wrapCreateBrowserRouterV7` function.
19+
To instrument your React Router, update your `Sentry.browserTracingIntegration` to `Sentry.reactRouterV7BrowserTracingIntegration` within `Sentry.init` and provide the required React hooks and router functions. Then, wrap the router instance created by `createBrowserRouter` or `createMemoryRouter` with one of the following functions:
4220

43-
</Alert>
21+
- Use `Sentry.wrapCreateBrowserRouterV7` for [`createBrowserRouter`](https://reactrouter.com/en/main/routers/create-browser-router) and [`createHashRouter`](https://reactrouter.com/en/main/routers/create-hash-router)
22+
- Use `Sentry.wrapCreateMemoryRouterV7` for [`createMemoryRouter`](https://reactrouter.com/en/main/routers/create-memory-router) (introduced in SDK version `8.50.0`)
4423

4524
```javascript {2-8, 15-21, 26-33}
4625
import React from "react";
@@ -76,9 +55,9 @@ const router = sentryCreateBrowserRouter([
7655
]);
7756
```
7857

79-
### Usage With `<Routes />` Component
58+
## Usage With `<Routes />` Component
8059

81-
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:
60+
If you're using the `<Routes />` component to define your routes, update your `Sentry.browserTracingIntegration` to `Sentry.reactRouterV7BrowserTracingIntegration` inside `Sentry.init` and provide the required React hooks and router functions. Then, wrap `<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.
8261

8362
```javascript {3-11, 18-24, 29, 33-35}
8463
import React from "react";
@@ -120,15 +99,14 @@ ReactDOM.render(
12099
);
121100
```
122101

123-
This is only needed at the top level of your app, rather than how v4/v5 required wrapping every `<Route/>` you wanted parametrized.
124-
125-
### Usage With `useRoutes` Hook
102+
This wrapper is only needed at the top level of your app, unlike React Router v4/v5, which required wrapping every `<Route />` you wanted parametrized.
126103

127-
If you specify your route definitions as an object to the [`useRoutes` hook](https://reactrouter.com/en/main/hooks/use-routes), use `Sentry.wrapUseRoutesV7` to create a patched `useRoutes` hook that instruments your routes with Sentry.
104+
## Usage With `useRoutes` Hook
128105

129-
<Alert level="warning">
106+
If you specify your route definitions as an object to the [`useRoutes` hook](https://reactrouter.com/en/main/hooks/use-routes), update your `Sentry.browserTracingIntegration` to `Sentry.reactRouterV7BrowserTracingIntegration` inside `Sentry.init` and provide the required React hooks and router functions. Then, use `Sentry.wrapUseRoutesV7` to create a patched `useRoutes` hook that instruments your routes with Sentry.
130107

131-
`wrapUseRoutesV7` 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).
108+
<Alert level="warning" title="Important">
109+
Call `wrapUseRoutesV7` outside of a React component, as in the example below. We also recommend that you assign the wrapped hook to a variable starting with `use`, as per [React's documentation](https://react.dev/learn/reusing-logic-with-custom-hooks#hook-names-always-start-with-use).
132110

133111
</Alert>
134112

@@ -174,16 +152,13 @@ ReactDOM.render(
174152
);
175153
```
176154

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.
155+
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, unlike React Router v4/v5, which required wrapping every `<Route />` you wanted parametrized.
178156

179-
### Custom Error Boundaries
157+
## Set Up a Custom Error Boundary
180158

181-
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.
182-
183-
<Alert>
184-
Note, that this only applies to render method and lifecycle errors since React
185-
doesn't need error boundaries to handle errors in event handlers.
186-
</Alert>
159+
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).\
160+
In production, these errors won't surface unless captured manually. If you **don't** have a custom error boundary in place, `react-router` will create a default one that "swallows" all errors.\
161+
Hence, to capture these errors with Sentry in production, we strongly recommend to implement a custom error boundary.
187162

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

@@ -227,7 +202,7 @@ export function YourCustomRootErrorBoundary() {
227202

228203
```
229204

230-
## Next Steps:
205+
## Next Steps
231206

232207
- [Return to **Getting Started**](../../)
233208
- [Return to the main integrations page](../)

0 commit comments

Comments
 (0)