TanStackT
TanStack5mo ago
2 replies
verbal-lime

Behaviour of retainSearchParams & stripSearchParams not what expected.

When using both retainSearchParams and stripSearchParams together, the default values are no longer stripped, and then its is not possible to navigate back to the default values after navigating away. Is this the expected behaviour or a bug?

For example the params in the url look like this by default

gallery?tag=all&sort=popularity&page=1

and when navigating to a different search param, I cannot navigate back to the default

const defaultSearchValues = {
  tag: 'all' as const,
  sort: 'popularity' as const,
  page: 1,
};

const gallerySearchSchema = z.object({
  id: z.number().optional(),
  searchTerm: z.string().optional(),
  tag: z
    .enum(['all', 'pro', 'competitive', 'funny', 'streamer', 'favorites'])
    .default(defaultSearchValues.tag),
  sort: z.enum(['popularity', 'newest']).default(defaultSearchValues.sort),
  page: z.number().default(defaultSearchValues.page),
});

export const Route = createFileRoute('/_header/valorant/crosshairs/gallery')({
  component: RouteComponent,
  validateSearch: zodValidator(gallerySearchSchema),
  search: {
    middlewares: [
      retainSearchParams(['id', 'searchTerm', 'tag', 'sort', 'page']),
      stripSearchParams(defaultSearchValues),
    ],
  },
Was this page helpful?