How to use completely optional search params? #923
-
I want these two URLs to both be valid: /create I got this far:
This works okay, but then if I try to navigate to the page, the "search" prop is required in typescript. Is there an easier way to do this? IMO it would be better if you could just return null or undefined in validateSearch, making the "search" prop optional when navigating |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
(I don't know if I understand your problem correctly. If not, please create a minimal reproducer.) You can tag the param type of a search validation function using see this example: and the docs: In your case, you could have only optional properties like this: validateSearch: (
input: {
foo?: string
bar?: 'string'
} & SearchSchemaInput,
) => ... Then when navigating to that page, the |
Beta Was this translation helpful? Give feedback.
-
Also see #2000 and https://tanstack.com/router/latest/docs/framework/react/guide/search-params#zod-adapter. |
Beta Was this translation helpful? Give feedback.
(I don't know if I understand your problem correctly. If not, please create a minimal reproducer.)
You can tag the param type of a search validation function using
SearchSchemaInput
.see this example:
https://github.com/TanStack/router/blob/main/examples/react/basic-default-search-params/src/main.tsx#L149
and the docs:
https://tanstack.com/router/v1/docs/api/router/RouteOptionsType#validatesearch
In your case, you could have only optional properties like this:
Then when navigating to that page, the
search
prop will not be required anymore.