question: How to do conditional useQuery?
See original GitHub issueHi, any idea, how to “elegantly” solve this one?
const { deckId } = useMyContext(); // <- this can be null during refreshing page
if (!deckId) return null;
const { props } = useQuery<cardsQuery>(query, { deckId }, {});
I’m obviously using conditions with hooks which will and up with the error: React Hook "useQuery" is called conditionally. React Hooks must be called in the exact same order in every component render.
The only way to go here is to create one more wrapping component, which will conditionally render this component. But then useQuery looks like I need even more complicated code then with QueryRenderer
What came into my mind as a solution is adding option supressQuery?: boolean which will allow me conditionally “turn off” the query.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:7 (5 by maintainers)
Top Results From Across the Web
How to call useQuery hook conditionally? - Stack Overflow
In apollo's documentation, it shows that there's a skip option you can add: useQuery(query,{skip:someState===someValue}).
Read more >React-Query: How to fetch queries conditionally - JS-HowTo
1- useQuery hook with enabled option ... Using this option we can control when to execute the fetch query by passing a boolean...
Read more >Need help / guide on best direction conditional access for my ...
How to use conditional access with a (very) slow internet connection? Will IP changes trigger reauthentication for Microsoft Conditional Access ...
Read more >Conditional Fetching | Redux Toolkit
Conditional Fetching. Overview. Query hooks automatically begin fetching data as soon as the component is mounted. But, there are use cases ...
Read more >Query parameter targeting - Optimize Resource Hub
When to use query parameter targeting; Benefits over URL targeting; Example: Target visitors ... Query parameters are found between the question mark (...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Thanks for the answer.
And there is one approach I will be using for now.
The
store-onlywill prevent sending the query to server at least and it will always returnnullwhich is exactly what I need.So for me that’s solved and feel free to reopen this issue if needed.
I have just released version 3.2.0 https://github.com/relay-tools/relay-hooks/releases/tag/v3.2.0
Feel free to open new issues if you find problems 😃 Thanks @stramel @melounek @roman-khasimyanov