T
TanStack•9mo ago
wise-white

Optionally replace param

I'm trying to optionally replace a route param (e.g. /$lang/... to /$anotherLang/...). In practice I'm looking for something similar to useParams({ strict: false }) but for setting parameters. The behaviour would be: - if the current URL has that param, replace it - if the current URL does not have the param, do nothing Is there something like this? Thanks --- The use case is a language selector in the footer which is supposed to replace the /$lang/ prefix of the current page.
11 Replies
foreign-sapphire
foreign-sapphire•9mo ago
when would a param not exist?
wise-white
wise-whiteOP•9mo ago
The param would exist in routes that start with /$lang and not exist in routes that start with /api for example.
foreign-sapphire
foreign-sapphire•9mo ago
you wouldn't navigate to API routes anyhow
wise-white
wise-whiteOP•9mo ago
We also have /admin which is not i18ned And those are normal routes I see what you are thinking I can see in your mind You are thinking... Put the footer logic only in the $lang.tsx layout file
foreign-sapphire
foreign-sapphire•9mo ago
so wouldn't using from: "/$lang" be the solution ?
wise-white
wise-whiteOP•9mo ago
🔮
foreign-sapphire
foreign-sapphire•9mo ago
haha almost
wise-white
wise-whiteOP•9mo ago
Seems to compile, thanks 💪 Yeah it compiles but if I do:
<Link from="/$lang" params={(prev) => ({ ...prev, lang: newLang })} />
<Link from="/$lang" params={(prev) => ({ ...prev, lang: newLang })} />
and then click from /en/abc/def then I get redirected to /es not /es/abc/def
foreign-sapphire
foreign-sapphire•9mo ago
ah with Link it is different (you mentioned useParams above)
foreign-sapphire
foreign-sapphire•9mo ago
we have an open issue for this https://github.com/TanStack/router/issues/2415
GitHub
Cannot update path params when navigating to '.' · Issue #2415 · ...
Which project does this relate to? Router Describe the bug I wrote a hook which should replace the slug in the URL if it doesn't match reality anymore. For example /projects/here-is-the-name-12...
foreign-sapphire
foreign-sapphire•9mo ago
that's how you would do it but this does not work right now

Did you find this page helpful?