Skip to content

Commit ed347b8

Browse files
onurtemizkanimatwawanaAbhiPrasad
authored
Document wrapUseRoutes from React Router 6 instrumentation. (#5475)
Co-authored-by: Isabel <[email protected]> Co-authored-by: Abhijeet Prasad <[email protected]>
1 parent ea7d4fb commit ed347b8

File tree

1 file changed

+60
-3
lines changed
  • src/platforms/javascript/guides/react/configuration/integrations

1 file changed

+60
-3
lines changed

src/platforms/javascript/guides/react/configuration/integrations/react-router.mdx

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ _(Available in version 7 and above)_
2222

2323
To use React Router v6 with Sentry:
2424

25-
1. Initialize `Sentry.reactRouterV6Instrumentation` as your routing instrumentation and provide the functions it needs to enable performance tracing:
25+
Initialize `Sentry.reactRouterV6Instrumentation` as your routing instrumentation and provide the functions it needs to enable performance tracing:
2626

2727
- `useEffect` hook from `react`
2828
- `useLocation` and `useNavigationType` hooks from `react-router-dom`
2929
- `createRoutesFromChildren` and `matchRoutes` functions from `react-router-dom` or `react-router` packages.
3030

31-
2. Wrap [`Routes`](https://reactrouter.com/docs/en/v6/api#routes-and-route) using `Sentry.withSentryReactRouterV6Routing` to create a higher order component, which will enable Sentry to reach your router context, as in the example below:
31+
### Usage With `<Routes />` Component
32+
33+
If you use the `<Routes />` component from `react-router-dom` to define your routes, wrap [`Routes`](https://reactrouter.com/docs/en/v6/api#routes-and-route) using `Sentry.withSentryReactRouterV6Routing`. This creates a higher order component, which will enable Sentry to reach your router context, as in the example below:
3234

3335
```javascript
3436
import React from 'react';
@@ -70,7 +72,62 @@ ReactDOM.render(
7072
);
7173
```
7274

73-
Now, Sentry should generate `pageload`/`navigation` transactions with parameterized transaction names (for example, /teams/:teamid/user/:userid), where applicable.
75+
### Usage With `useRoutes` Hook
76+
_(Available in version 7.12.1 and above)_
77+
78+
If you specify your route definitions as an object to the [`useRoutes` hook](https://reactrouter.com/en/main/hooks/use-routes), use `Sentry.wrapUseRoutes` to create a patched `useRoutes` hook that instruments your routes with Sentry.
79+
80+
<Alert level="warning">
81+
82+
`wrapUseRoutes` 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).
83+
84+
</Alert>
85+
86+
```javascript
87+
import {
88+
createRoutesFromChildren,
89+
matchRoutes,
90+
useLocation,
91+
useNavigationType,
92+
useRoutes,
93+
} from "react-router-dom";
94+
import { wrapUseRoutes } from "@sentry/react";
95+
import { useEffect } from "react";
96+
97+
Sentry.init({
98+
dsn: "__DSN__",
99+
integrations: [
100+
new BrowserTracing({
101+
routingInstrumentation: Sentry.reactRouterV6Instrumentation(
102+
useEffect,
103+
useLocation,
104+
useNavigationType,
105+
createRoutesFromChildren,
106+
matchRoutes,
107+
),
108+
}),
109+
],
110+
tracesSampleRate: 1.0,
111+
});
112+
113+
const useSentryRoutes = wrapUseRoutes(useRoutes);
114+
115+
function App() {
116+
return useSentryRoutes([
117+
// ...
118+
]);
119+
}
120+
121+
ReactDOM.render(
122+
<BrowserRouter>
123+
<App />
124+
</BrowserRouter>,
125+
document.getElementById("root"),
126+
);
127+
```
128+
129+
130+
Now, Sentry should generate `pageload`/`navigation` transactions with parameterized transaction names (for example, `/teams/:teamid/user/:userid`), where applicable.
74131

75132

76133
## React Router v4/v5

0 commit comments

Comments
 (0)