Is there an existing issue for this?
How do you use Sentry?
Sentry Saas (sentry.io)
Which SDK are you using?
@sentry/react
SDK Version
7.68.0
Framework Version
React 18.2.0
Link to Sentry event
No response
SDK Setup
No response
Steps to Reproduce
When using wrapUseRoutes we noticed that route components were being unmounted and remounted if the component where useRoutes is called was re-rendered. After some digging we realised it is because each time the wrapped useRoutes function is called, a new SentryRoutes component will be created (i.e. a totally new function component, not a re-rendering of the component) - https://github.com/getsentry/sentry-javascript/blob/develop/packages/react/src/reactrouterv6.tsx#L231
- Set up a React project using React Router v6 and Sentry, using
wrapUseRoutes
- In the component that renders the element returned from
useRoutes, add a useState and a button to trigger setting the state, which will force the component to re-render.
- Add a
useEffect with no dependencies in a route component to log when it mounts / un-mounts.
- Cause the outer component to re-render and notice that the route component un-mounts and re-mounts.
- Remove the use of
wrapUseRoutes and repeat, noting that the route component remains mounted.
Expected Result
Routes behave the same whether using React Router's useRoutes or one wrapped using wrapUseRoutes.
Actual Result
Route components un-mount and re-mount each time the component containing the call to useRoutes re-renders.
Is there an existing issue for this?
How do you use Sentry?
Sentry Saas (sentry.io)
Which SDK are you using?
@sentry/react
SDK Version
7.68.0
Framework Version
React 18.2.0
Link to Sentry event
No response
SDK Setup
No response
Steps to Reproduce
When using
wrapUseRouteswe noticed that route components were being unmounted and remounted if the component whereuseRoutesis called was re-rendered. After some digging we realised it is because each time the wrappeduseRoutesfunction is called, a newSentryRoutescomponent will be created (i.e. a totally new function component, not a re-rendering of the component) - https://github.com/getsentry/sentry-javascript/blob/develop/packages/react/src/reactrouterv6.tsx#L231wrapUseRoutesuseRoutes, add auseStateand a button to trigger setting the state, which will force the component to re-render.useEffectwith no dependencies in a route component to log when it mounts / un-mounts.wrapUseRoutesand repeat, noting that the route component remains mounted.Expected Result
Routes behave the same whether using React Router's
useRoutesor one wrapped usingwrapUseRoutes.Actual Result
Route components un-mount and re-mount each time the component containing the call to
useRoutesre-renders.