|
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 | 7 | <Alert level="info" title="Looking for framework mode?"> |
8 | 8 | React Router v7 (framework mode) is currently in experimental Alpha, check out |
9 | 9 | the docs [here](/platforms/javascript/guides/react-router/). |
10 | 10 | </Alert> |
| 11 | +Apply the following setup steps based on your routing method and create a |
| 12 | +[custom error boundary](#custom-error-boundaries) to make sure Sentry |
| 13 | +automatically captures rendering errors. |
11 | 14 |
|
12 | | -_React Router v7 (library mode) support is included in the `@sentry/react` package since version `8.42.0`._ |
| 15 | +## Usage with `createBrowserRouter` or `createMemoryRouter` |
13 | 16 |
|
14 | | -### Usage with `createBrowserRouter` or `createMemoryRouter` |
15 | | - |
16 | | -To instrument your React Router, update 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: |
| 17 | +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: |
17 | 18 |
|
18 | 19 | - 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) |
19 | 20 | - Use `Sentry.wrapCreateMemoryRouterV7` for [`createMemoryRouter`](https://reactrouter.com/en/main/routers/create-memory-router) (introduced in SDK version `8.50.0`) |
@@ -52,9 +53,9 @@ const router = sentryCreateBrowserRouter([ |
52 | 53 | ]); |
53 | 54 | ``` |
54 | 55 |
|
55 | | -### Usage With `<Routes />` Component |
| 56 | +## Usage With `<Routes />` Component |
56 | 57 |
|
57 | | -If you're using the `<Routes />` component to define your routes, update 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. |
| 58 | +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. |
58 | 59 |
|
59 | 60 | ```javascript {3-11, 18-24, 29, 33-35} |
60 | 61 | import React from "react"; |
@@ -96,11 +97,11 @@ ReactDOM.render( |
96 | 97 | ); |
97 | 98 | ``` |
98 | 99 |
|
99 | | -This wrapping is only needed at the top level of your app, unlike React Router v4/v5, which required wrapping every `<Route />` you wanted parametrized. |
| 100 | +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. |
100 | 101 |
|
101 | | -### Usage With `useRoutes` Hook |
| 102 | +## Usage With `useRoutes` Hook |
102 | 103 |
|
103 | | -If you specify your route definitions as an object to the [`useRoutes` hook](https://reactrouter.com/en/main/hooks/use-routes), update 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. |
| 104 | +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. |
104 | 105 |
|
105 | 106 | <Alert level="warning" title="Important"> |
106 | 107 | 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). |
@@ -151,7 +152,7 @@ ReactDOM.render( |
151 | 152 |
|
152 | 153 | 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. |
153 | 154 |
|
154 | | -### Custom Error Boundaries |
| 155 | +## Custom Error Boundaries |
155 | 156 |
|
156 | 157 | 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. |
157 | 158 |
|
@@ -202,7 +203,7 @@ export function YourCustomRootErrorBoundary() { |
202 | 203 |
|
203 | 204 | ``` |
204 | 205 |
|
205 | | -## Next Steps: |
| 206 | +## Next Steps |
206 | 207 |
|
207 | 208 | - [Return to **Getting Started**](../../) |
208 | 209 | - [Return to the main integrations page](../) |
0 commit comments