T
TanStack•2y ago
vicious-gold

Get params without including it in file name.

Hi! I have an URL like that: http://localhost:5173/auth/login?error=already_exists I want to get the error value without having the $error in my file name.
3 Replies
fascinating-indigo
fascinating-indigo•2y ago
You can use a useSearch() hook to pull out the search parameter from the URL for that route. https://tanstack.com/router/latest/docs/framework/react/guide/search-params
Search Params | TanStack Router 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 URLSearchParams?
fascinating-indigo
fascinating-indigo•2y ago
It'd be best to define your validation schema for the search params to get type-safe usage.
import { z } from 'zod';

export const Route = createFileRoute('...')({
validateSearch: search => z.object({
error: z.string().optional(),
}).parse(search)
})
import { z } from 'zod';

export const Route = createFileRoute('...')({
validateSearch: search => z.object({
error: z.string().optional(),
}).parse(search)
})
vicious-gold
vicious-goldOP•2y ago
okay ! 🙂

Did you find this page helpful?