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
quickest-silver•12mo ago
we have a concept named "search middlewares" exactly for that purpose
quickest-silver•12mo 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...
quickest-silver•12mo ago
=>
stripSearchParamscorrect-apricotOP•12mo ago
oh, damn, I missed that in the bottom, thanks, I'll check it out.
quickest-silver•12mo ago
we have lots of features, so I can absolutely understand if they are bit hard to discover
correct-apricotOP•12mo ago
Cool, it works perfectly:
quickest-silver•12mo ago
great!
correct-apricot•5mo 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.rival-black•5mo ago
I think you could also do default in typescript vs in zod
const { sort = "a-z". page = 1 ... } = Route.useSearch();
flat-fuchsia•5mo ago
Wanted to achieve something similar without luck. So finally I removed the "defaults" params