DINO
DINO9mo ago

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
})
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.
1 Reply
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View