T
TanStack•2y ago
rare-sapphire

How to make search params optional and rely on zod .catch() for default ?

Hello ! When defining routes we can use validateSearch combined with zod schemas in order to type the route search, however this schema cannot be undefined and while we can set defaults with catch(), we are still required to provide said search params when navigating with Link or navigate() and it is a bit tedious Is there a clean approach to make search params optionnal for navigation ? Cheers ! 🙂
4 Replies
ratty-blush
ratty-blush•2y ago
my approach to this is to leave all search params optional, but there's a different way with SearchSchemaInput: https://tanstack.com/router/v1/docs/framework/react/api/router/SearchSchemaInputType
SearchSchemaInput type | TanStack Router React Docs
The SearchSchemaInput type is used to tag the input type of a validateSearch method to signalize to TanStack router that its parameter type TSearchSchemaInput shall be used to type the search param of and navigate().
ratty-blush
ratty-blush•2y ago
GitHub
router/examples/react/basic-default-search-params/src/main.tsx at f...
🤖 Fully typesafe Router for React (and friends) w/ built-in caching, 1st class search-param APIs, client-side cache integration and isomorphic rendering. - TanStack/router
rare-sapphire
rare-sapphireOP•2y ago
this is great ! thank you so much for such a fast and clear answer, with exemples as always, you're the best 💌
fair-rose
fair-rose•2y ago
Perfect! This is exactly what I've been looking for! Thanks so much!

Did you find this page helpful?