TanStackT
TanStack9mo ago
14 replies
brilliant-lime

Hooks in shared component (useParams, useSearch)

Has anyone found a good solution for using type-safe hooks (useParams, useSearch) from components that could be mounted on multiple routes? I'm thinking of writing custom hooks that take an array of
from
instead of a single
from
string, but I'd love to know if someone has something better.

The basic problem is something like this:
const Shared = () => {
  const { id } = useParams({ from: '???' })
}

createFileRoute('/foo/$id')({
  component: Shared
}}

createFileRoute('/bar/$id')({
  component: Shared
}}


(Basically a component where the search params or path params are actually present in multiple routes, but just how do you get that in a type-safe way, i.e. without strict:false)
Was this page helpful?