|
1 | 1 | ---
|
2 | 2 | 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." |
4 | 4 | sidebar_order: 10
|
5 | 5 | ---
|
6 | 6 |
|
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/). |
31 | 10 | </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: |
32 | 14 |
|
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"]} /> |
38 | 16 |
|
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` |
40 | 18 |
|
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: |
42 | 20 |
|
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`) |
44 | 23 |
|
45 | 24 | ```javascript {2-8, 15-21, 26-33}
|
46 | 25 | import React from "react";
|
@@ -76,9 +55,9 @@ const router = sentryCreateBrowserRouter([
|
76 | 55 | ]);
|
77 | 56 | ```
|
78 | 57 |
|
79 |
| -### Usage With `<Routes />` Component |
| 58 | +## Usage With `<Routes />` Component |
80 | 59 |
|
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. |
82 | 61 |
|
83 | 62 | ```javascript {3-11, 18-24, 29, 33-35}
|
84 | 63 | import React from "react";
|
@@ -120,15 +99,14 @@ ReactDOM.render(
|
120 | 99 | );
|
121 | 100 | ```
|
122 | 101 |
|
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. |
126 | 103 |
|
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 |
128 | 105 |
|
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. |
130 | 107 |
|
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). |
132 | 110 |
|
133 | 111 | </Alert>
|
134 | 112 |
|
@@ -174,16 +152,13 @@ ReactDOM.render(
|
174 | 152 | );
|
175 | 153 | ```
|
176 | 154 |
|
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. |
178 | 156 |
|
179 |
| -### Custom Error Boundaries |
| 157 | +## Set Up a Custom Error Boundary |
180 | 158 |
|
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. |
187 | 162 |
|
188 | 163 | To send errors to Sentry while using a custom error boundary, use the `Sentry.captureException` method:
|
189 | 164 |
|
@@ -227,7 +202,7 @@ export function YourCustomRootErrorBoundary() {
|
227 | 202 |
|
228 | 203 | ```
|
229 | 204 |
|
230 |
| -## Next Steps: |
| 205 | +## Next Steps |
231 | 206 |
|
232 | 207 | - [Return to **Getting Started**](../../)
|
233 | 208 | - [Return to the main integrations page](../)
|
0 commit comments