How to call useQuery conditionally?

Or How to call query like mutate?
13 Replies
.snkt
.snktOP2y ago
I only want to call the download endpoint on clicking download button. and I don't need the functionality to cache and auto refetch. something like
const {query} = useQuery()
fun onclick = async () => {
const data = await query(resourceLocation)
}
const {query} = useQuery()
fun onclick = async () => {
const data = await query(resourceLocation)
}
awexis
awexis2y ago
Why not make it a mutation
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
.snkt
.snktOP2y ago
Is that recommended? I think it does not matter as long as it gets the work done
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
awexis
awexis2y ago
Yeah since it seems like you don't want it to fetch once link exists right?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
.snkt
.snktOP2y ago
I did. but its gets complicated down the road.
.snkt
.snktOP2y ago
simple and clear. Thanks.. @awexis
awexis
awexis2y ago
Dependent Queries | TanStack Query Docs
Dependent (or serial) queries depend on previous ones to finish before they can execute. To achieve this, it's as easy as using the enabled option to tell a query when it is ready to run:
awexis
awexis2y ago
Here is how you would do it if you want it to be like "once link exist fetch the data" for the future Glad it helped!
whatplan
whatplan2y ago
have used this before, easy and useful
const { data } = useQuery({ id: id_or_undef }, {
enabled: !!id_or_undef
})
const { data } = useQuery({ id: id_or_undef }, {
enabled: !!id_or_undef
})
awexis
awexis2y ago
Yup that's what the link does :)
Want results from more Discord servers?
Add your server