T
TanStack•12mo ago
stormy-gold

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
grumpy-cyan
grumpy-cyan•12mo ago
when would a param not exist?
stormy-gold
stormy-goldOP•12mo ago
The param would exist in routes that start with /$lang and not exist in routes that start with /api for example.
grumpy-cyan
grumpy-cyan•12mo ago
you wouldn't navigate to API routes anyhow
stormy-gold
stormy-goldOP•12mo 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
grumpy-cyan
grumpy-cyan•12mo ago
so wouldn't using from: "/$lang" be the solution ?
stormy-gold
stormy-goldOP•12mo ago
🔮
grumpy-cyan
grumpy-cyan•12mo ago
haha almost
stormy-gold
stormy-goldOP•12mo 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
grumpy-cyan
grumpy-cyan•12mo ago
ah with Link it is different (you mentioned useParams above)
grumpy-cyan
grumpy-cyan•12mo 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...
grumpy-cyan
grumpy-cyan•12mo ago
that's how you would do it but this does not work right now

Did you find this page helpful?