DINOD
Reactiflux2y ago
3 replies
DINO

How do I query data with Tanstack Query if one of the query params can be undefined.

Hello,

I have an
id
variable that can be undefined because I get him from
useParams
, and I use the
id
value to query data with Tanstack Query, but Tanstack Query requires to have a value that cannot be undefined which is very logical. So I had to do
Number(id) || 1
and
enabled: !!id
this way:

const { id } = useParams() // const id: string | undefined
const { data: counterSale } = trpcReact.counterSale.get.useQuery(Number(id) || 1, {
    enabled: !!id
})

Is this a common practice? And is there any other way to do this?

Thanks.
Was this page helpful?