T
TanStack•3y ago
conscious-sapphire

Tracked Queries and Providers

When fetching data with a useQuery but doing so in a provider to pass this data around, does doing this ruin tracking and make react-query watch all variables? Can you also give a good example of when and when not to be using queries in a provider. We typically call them in a provider, mix with some other logic and return data. Should this 'mix with other logic' typically happen within the queryFn and requirements for Provider passing the data is more of the edge case?
6 Replies
flat-fuchsia
flat-fuchsia•3y ago
You can compose a custom hook out of useQuery. Maybe this helps
foreign-sapphire
foreign-sapphire•3y ago
you usually don't need to put data from react-query into a context. why would you do that? (there are reasons, but they are rare)
conscious-sapphire
conscious-sapphireOP•3y ago
@TkDodo 🔮 That's sort've the answer I was looking for with potentially any reason why we would need it in a provider? IE: Currently we may mix data responses with provider logic to return the context value. You say usually therefore making me think there is a caveat? 😄
foreign-sapphire
foreign-sapphire•3y ago
I mean I do have a blogpost on the topic 😉 https://tkdodo.eu/blog/react-query-and-react-context
React Query and React Context
Can it make sense to combine React Query with React Context ? Yes, sometimes ...
conscious-sapphire
conscious-sapphireOP•3y ago
It's a good blog post and helps for general guidance but was wondering in terms of tracked queries specifically, if I pass 'data' directly from the userQuery and then access a variable from context, is that tracked or will it rerender on any change to data now? Cheers!
foreign-sapphire
foreign-sapphire•3y ago
we only track top level properties, so data. this is independent of context

Did you find this page helpful?