T
TanStack14mo ago
like-gold

Navigate to the same route but change the route param

Hello, is it possible to change the route param? My use case is, I have something like: /$companyId/settings /$companyId/users /$companyId/files What I want is to add a select on the page so at any moment the user can choose another company to see the details, so I want to implement a generic way to change the route param for the current route, how can I do that?
16 Replies
protestant-coral
protestant-coral14mo ago
Not sure if you have completed this, but I think you would have like a select or whatever, to change the companyID, and on selection you would navigate to 1/settings you would need to push to that /settings page and so on
inland-turquoise
inland-turquoise14mo ago
You can use navigate() and only pass path params to change.
like-gold
like-goldOP14mo ago
I didn't notice that I could pass only params without the to, thank you @Tanner Linsley, that's exactly what I want. @Enyel The option that Tanner gave me solved the problem, but thank you as well. I found now that navigate without to does not change the params, is that a bug? nvm, my bad.
fair-rose
fair-rose11mo ago
@Tanner Linsley is there a good way to call the navigate with only the path params without TS complaining becase it cannot infor the supported path params for the current route? If I use navigate({ params: { companyId: value }}); it complains that Object literal may only specify known properties, and companyId does not exist in type If I use navigate({ params: (prev) => ({...prev, companyId: value })}); it complains that the { companyId: string } is not assignable to type never Thanks in advance for the help (and great tool).
inland-turquoise
inland-turquoise11mo ago
Supply a from property
fair-rose
fair-rose11mo ago
Isn't that basically the same as providing a to? I need to know the route path to be able to have the correct type, using a from /$companyId navigates to it instead of keeping the current path (e.g. /$companyId/users). I could only make it work using a path obtained from the mathes (should also work with the from):
const matches = useMatches();
const to = matches[matches.length - 1].fullPath;

navigate({to, params: { companyId });
const matches = useMatches();
const to = matches[matches.length - 1].fullPath;

navigate({to, params: { companyId });
vicious-gold
vicious-gold11mo ago
can you please provide a complete minimal example ?
fair-rose
fair-rose11mo ago
Tiago Pocinho
StackBlitz
Router Start Basic Example (forked) - StackBlitz
Run official live example code for Router Start Basic, created by Tanstack on StackBlitz
fair-rose
fair-rose11mo ago
@Manuel Schiller was the example clear enough? Any suggestion on what is the correct approach here? Thanks in advance.
vicious-gold
vicious-gold10mo ago
can you please create a GitHub issue to properly track this?
fair-rose
fair-rose10mo ago
Sure, I will create one
metropolitan-bronze
metropolitan-bronze10mo ago
Checked out this issue and with the latest versions, the runtime behaviour is correctly working. We've always maintained that you need to supply from or to to actually get type-safety. The issue is that without the from or to, when you are making an update to the params, you are kind of going in blind, where the router has figure out the current route to selectively update the params. This works during runtime, but doesn't work for the types. @Manuel Schiller @Chris Horobin When no from and to are supplied and you try to call navigate, what's the behaviour we expect to happen? It seems like something that'd slow down tsc.
foreign-sapphire
foreign-sapphire10mo ago
Yeah, we want to try to make it explicit where you're opting out of type safety. TypeScript is completely in the dark if you don't provide either of these but there are explicit opt outs like to='.'
fair-rose
fair-rose10mo ago
In this scenario, using the to='.' does not work as the new parameter value is not applied at all. I have added that example as well for completeness.
vicious-gold
vicious-gold10mo ago
so it is a runtime bug then?
fair-rose
fair-rose10mo ago
I missed you message. It depends, if the solution is not to pass a to or a from, then it is a TS issue, if the solution is to pass a to or a from, then it is a runtime issue.

Did you find this page helpful?