Closed
Description
I'm using React Router as a...
library
Reproduction
β What is the issue?
When testing a simple KeepAlive(Just implemented it by DOM visibility) demo with React Router, I encountered unexpected multiple re-renders of route components. This happens both with Data Routers and Declarative Routers. The number of times a component re-renders seems to correspond with the number of open tabs/pages β which is highly unintuitive and seems like a bug.
π Reproduction Behavior
- Each time I navigate to a new route (e.g., open a detail page like
blogs/:id
), it renders once per open tab. - For example, if I open 3 different
blogs/:id
pages in tabs, switching between them triggers 3 renders of the current route component. - Iβve confirmed this by logging
console.log('render')
inside the component.
πΉ Evidence (Video or Screenshot)
video_1280.mp4
π Steps to Reproduce
- Setup a minimal React Router demo using either
createBrowserRouter
orRoutes
+Route
. - Create a dynamic route such as
/blogs/:id
. - Open multiple KeepAlive tabs, then navigate between blog details.
- Add a
console.log('render')
in the blog detail component. - Observe multiple re-renders on a single navigation.
π§ Investigations Done
- Tried both declarative and data routers β same issue.
- Confirmed the behavior in both development and production builds.
- No suspicious side effects or context updates in parent components.
- Ensured no strict mode double renders are involved.
π€ Questions
- Is this an expected behavior in React Router, or a known limitation with tab-based routing and re-renders?
- Is there any recommended pattern to prevent redundant re-renders in such cases?
- Could this be related to caching or reuse strategies within the router context?
π§© Reproduction Repo
- GitHub repo
- CodeSandbox
System Info
System:
OS: macOS 15.5
CPU: (8) arm64 Apple M2
Memory: 108.61 MB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.19.0 - ~/.nvm/versions/node/v20.19.0/bin/node
npm: 10.8.2 - ~/.nvm/versions/node/v20.19.0/bin/npm
pnpm: 10.8.1 - ~/.local/share/pnpm/pnpm
Browsers:
Chrome: 137.0.7151.104
Safari: 18.5
Used Package Manager
pnpm
β Expected Behavior
Each route/component should render only once per navigation (unless something explicitly causes it to re-render).
β Actual Behavior
The route re-renders once per open tab, seemingly without state or prop changes.