You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Params changed in child components seem to cause parent components subscribed to useParams to re-render.
The bug occurs in the component $artist.tsx when navigating between child routes (album.tsx).
The parent doesn't have access to the child param as surfaced by TypeScript. The artist component re-renders every time you switch between albums, even though it only uses the parameter $artist
Workaround
The example repo shows the solution in a commented line:
constartist=Route.useParams({select: ({ artist })=>artist});// Prevents unnecessary re-renders, but is boilerplate
Using the select option tells the hook to only subscribe to changes fixes the issue but is boilerplate that the developer has to remember.
The issue happens by default when using Route.useParams without a select function to narrow down the selector
//$artist.tsxconst{ artist }=Route.useParams();// Will re-render even if album changes, even though album is in-accessible here
This hook subscribes to all parameter changes in the route tree, including child route parameters . So when navigating from /Some artist/Album1 to /Some artist/Album2, the hook triggers a re-render even though the $artist parameter didn't change.
Expected behavior
Navigating between albums (Album1 → Album2) should NOT cause the parent artist component to re-render since the parameter remains the same $artist
Screenshots or Videos
The default behavior const { artist } = Route.useParams();
Default.Behavior.webm
With const artist = Route.useParams({ select: ({ artist }) => artist });
Which project does this relate to?
Router
Describe the bug
Params changed in child components seem to cause parent components subscribed to
useParamsto re-render.The bug occurs in the component
$artist.tsxwhen navigating between child routes (album.tsx).The parent doesn't have access to the child param as surfaced by TypeScript. The artist component re-renders every time you switch between albums, even though it only uses the parameter
$artistWorkaround
The example repo shows the solution in a commented line:
Using the
selectoption tells the hook to only subscribe to changes fixes the issue but is boilerplate that the developer has to remember.Your Example Website or App
https://github.com/AshotN/TanStack-Router-useParamsBug
Steps to Reproduce the Bug or Issue
The issue happens by default when using
Route.useParamswithout aselectfunction to narrow down the selectorThis hook subscribes to all parameter changes in the route tree, including child route parameters . So when navigating from
/Some artist/Album1to/Some artist/Album2, the hook triggers a re-render even though the$artistparameter didn't change.Expected behavior
Navigating between albums (
Album1→Album2) should NOT cause the parent artist component to re-render since the parameter remains the same$artistScreenshots or Videos
The default behavior
const { artist } = Route.useParams();Default.Behavior.webm
With
const artist = Route.useParams({ select: ({ artist }) => artist });WithSelect.webm
Platform
Additional context
No response