Skip to content

RR6 wrapUseRoutes creates a new component on every render #8962

Closed
@jameshoward

Description

@jameshoward

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

  1. Set up a React project using React Router v6 and Sentry, using wrapUseRoutes
  2. 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.
  3. Add a useEffect with no dependencies in a route component to log when it mounts / un-mounts.
  4. Cause the outer component to re-render and notice that the route component un-mounts and re-mounts.
  5. 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.

Metadata

Metadata

Assignees

Labels

Package: reactIssues related to the Sentry React SDK

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions