I am using useQuery from react-query to fetch data that I only want the query to run in some condition. How can I use it?
following is my code to use useQuery
const query = useQuery<APIResponse, Error>(
[{query: creatGQL, variables: variables}],
async () => {
const result: APIResponse = await ucFetch(apiUrl, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
query: creatGQL,
variables: variables,
}),
});
return result;
} );
return query;
A query can be disabled / enabled by using the enabled option. If it is false, the query will not run:
useQuery(key, queryFn, { enabled: myCondition }
the condition can be static, or driven by state / props - anything that's a boolean really.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With