How to make default search params to not be populated when visiting page.
I have a page "/my-files" with a bunch of search params where each parameter have a default value. I have a link to this page looking like so:
<Link to="/my-files">
. When clicking on this, the URL get's populated with the default search params so it looks like this: /my-files?sort=a-z&page=1&pageSize=10&query=
. I would like the URL to be kept as /my-files and only have the URL be populated when a search param is different than the default.
Is what I want to do an anti-pattern of sorts?
This is my route configuration:
10 Replies
flat-fuchsia•9mo ago
we have a concept named "search middlewares" exactly for that purpose
flat-fuchsia•9mo ago
Search Params | TanStack Router React Docs
Similar to how TanStack Query made handling server-state in your React applications a breeze, TanStack Router aims to unlock the power of URL search params in your applications. Why not just use URLSe...
flat-fuchsia•9mo ago
=>
stripSearchParams
absent-sapphireOP•9mo ago
oh, damn, I missed that in the bottom, thanks, I'll check it out.
flat-fuchsia•9mo ago
we have lots of features, so I can absolutely understand if they are bit hard to discover
absent-sapphireOP•9mo ago
Cool, it works perfectly:
flat-fuchsia•9mo ago
great!
genetic-orange•2mo ago
Just discovering
stripSearchParams
and it seems exactly what I need! However, I'm finding that <Link>
always is active even if the search param is different from the default. I can get around this by using exact: true
but then that would break certain use cases. Not sure if this is expected or not, or if there is a different solution that would work better here.flat-fuchsia•2mo ago
I think you could also do default in typescript vs in zod
const { sort = "a-z". page = 1 ... } = Route.useSearch();
sensitive-blue•2mo ago
Wanted to achieve something similar without luck. So finally I removed the "defaults" params