question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

question: How to do conditional useQuery?

See original GitHub issue

Hi, 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:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
melounekcommented, Mar 5, 2020

Thanks for the answer.

And there is one approach I will be using for now.

const { deckId } = useMyContext();
const { props } = useQuery<cardsQuery>(
    query,
    { deckId: String(deckId) },
    { fetchPolicy: !deckId ? "store-only" : "store-or-network" }
  );

The store-only will prevent sending the query to server at least and it will always return null which is exactly what I need.

So for me that’s solved and feel free to reopen this issue if needed.

6reactions
morryscommented, Mar 15, 2020

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found