T
TanStack•2y ago
passive-yellow

React - queryClient.getQueryData(key) - can I think about it as about useContext?

I have a situation where I fetch data in <ParentComponent /> using useQuery hook. I have a lot of nested children in this component and I need to display the data on the bottom of the tree and I consider following options: - props drilling - sounds bad as usual props drilling 🙂 - context - there are performance issues with the context without using useContextSelector and it's not compliant for some reason. - queryClient.getQueryData as a mechanism to retrieve in the bottom of the tree or throw when undefined. Considering only scenario 3: 1) Is it correct to assume data will be always defined when fetched?\ 2) Does it break somehow react-query paradigms?
2 Replies
conscious-sapphire
conscious-sapphire•2y ago
getQueryData is an imperative method that doesn't create a subscription. Your component will not re-render when new data comes in. You should only use this function in callbacks. You'd usually just want to call useQuery again in the children that need it.
passive-yellow
passive-yellowOP•2y ago
I've just read your blog on staletime and sounds like prefetch is my way to go. Thanks for your help 🙂

Did you find this page helpful?