#Default search which is always defined

12 messages · Page 1 of 1 (latest)

devout ferry
#

Hello

Is it possible to create a default search so useSearch always returns a defined value AND so that navigations to the path don't require search params?

#

Default search which is always defined

spare ruin
#

Schema validators usually have a "default value" method. For example w/ zod it's { foo: z.string().default('bar') }

#

(not all of them have this, i think yup doesn't for example)

#

if you use that in the validateSearch of your route, then the search param is not required (navigations to the path don't require search params) and useSearch has it defined (useSearch always returns a defined value)

devout ferry
#

@spare ruin yes, but how to do it without a validator?

#

I would have expected that inside validateSearch, assigning a default value would solve this issue. it doesn't (navigation still requires search), unless I'm doing something wrong

spare ruin
#

sorry idk how you do it without a validator

#

just tried this

  validateSearch: ({hello}: {hello?: string}) => ({
    hello: hello ?? 'world',
  }),

and it doesn't work. Might be a bug... not sure

split grove
#

Optionally, the parameter type can be tagged with the SearchSchemaInput type like this: (searchParams: TSearchSchemaInput & SearchSchemaInput) => TSearchSchema. If this tag is present, TSearchSchemaInput will be used to type the search property of <Link /> and navigate() instead of TSearchSchema. The difference between TSearchSchemaInput and TSearchSchema can be useful, for example, to express optional search parameters.