When to use fetchQuery? #8553
Unanswered
coltanium13
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey @TkDodo ! I was hoping to get some advice from you.
I generally never use fetchQuery, but i find myself needing to use it i think. However, im apprehensive because i cant find anything referencing when to use fetchQuery, other than a few cases with react router and your one post on stack overflow.
I have provider that uses a custom hook that basically listens for an event to happen globally. At first i just used useQuery for several hooks, and disabled them until the particular event occurred, and changed enabled to true to fire off the query. At the same time, a useEffect would be there and run when the query results come back, and then make the decisions based on that.
However, it seems really messy/weird with all the disabled queries, and useEffect that triggers the decision making on where to history.push based on the query that fired. Im not rendering anything where these useQuery hooks are used, just need to get data. It's a hook that needs to run logic when a global event happens more or less. Then, i route to a new page based on logic. (scan this thing, then go to that page).
if i use fetchQuery, i can just have a function that is called when the global event happens, then fire off the correct fetchQuery api based on the global event that happened. No useEffect or always having disabled queries in the app until the event occurs(possibly 4 different useQuery hooks waiting to be enabled).
Tldr; When is using fetchQuery acceptable? Is using it in a custom hook a fine solution? Or is it best to disable the useQuery and re enable it then run the logic in a useEffect after the data comes back?
Beta Was this translation helpful? Give feedback.
All reactions