T
TanStack10mo ago
genetic-orange

useQuery does not re-render when using in 2 separate components at the same time

I have 2 components, in the tree they live at the same time (not always, but by default, they do). One component call it the "header", lives always, and when a prop changes, it triggers the useQuery hook and fetches data. All good here. Second component, call it "consumer", lives sometimes, and needs access to that fetched data. This one calls useQuery with "enabled" set to false and has the same queryKey as the header does. The problem is, when header finishes fetching, consumer doesn't re-render, it doesn't get the newly fetched data until I manually remount it after header finished fetching. Been trying to solve this for 3 hours and I am out of ideas... plz help
12 Replies
fair-rose
fair-rose10mo ago
This one calls useQuery with "enabled" set to false and has the same queryKey as the header does.
That doesn't sound right… can you share some code?
genetic-orange
genetic-orangeOP10mo ago
No description
genetic-orange
genetic-orangeOP10mo ago
this is the main useQuery that fetches data in header
genetic-orange
genetic-orangeOP10mo ago
this is the consumer. Simplified to the maximum so that I can debug the issue
No description
genetic-orange
genetic-orangeOP10mo ago
i've been trying with "refetchOnMount" instead of enabled but nothing. At this point im trying things blindly If i remove enabled, but keep staletime, the query will fetch twice
fair-rose
fair-rose10mo ago
I would just declare the containerBaseData once and use it in both components
optimistic-gold
optimistic-gold10mo ago
please don't create useQuery instances without a queryFn. Extract at least key and fn, possibly also staleTime to some shared options and re-use those enabled: false seems unnecessary, especially with staleTime: Infinity
If i remove enabled, but keep staletime, the query will fetch twice
this is fishy because with staleTime: Infinity, you'll always get cached data without a refetch if it exists. Maybe you have an unstable query cache, hard to say. But this is definitely not how react-query should behave. I can take a deeper look at a standalone reproduction (stackblitz, codesandbox)
genetic-orange
genetic-orangeOP10mo ago
I am really sorry, I figured out the issue. The problem was I declared const queryClient = new QueryClient(); inside the component instead of outside.. I was probably rushing or tired.. Once I put it outside the component everything is good and I can go back to how react query is supposed to be used I thought about it, but first I wanted to find the root cause for my issue as I know react query shouldn't behave as it was. All fixed now also thanks @TkDodo 🔮 for the tips!
optimistic-gold
optimistic-gold10mo ago
we have a lint rule for that because it's so common. Please use it: https://tanstack.com/query/v5/docs/eslint/stable-query-client
TanStack | High Quality Open-Source Software for Web Developers
Headless, type-safe, powerful utilities for complex workflows like Data Management, Data Visualization, Charts, Tables, and UI Components.
From An unknown user
From An unknown user
genetic-orange
genetic-orangeOP10mo ago
you guys think of everything lol, I just thought "there probably should be a rule for idiots like me"
optimistic-gold
optimistic-gold10mo ago
sorry to say but usually, the overlap between people who get this wrong and people who know about the lint rules is converging towards zero 😅 which makes the rule a bit unnecessary but yeah, we have it 🚀
genetic-orange
genetic-orangeOP10mo ago
awesome, thanks again! It's funny because I know it was supposed to be stable, but I just didn't do it for unknown reasons

Did you find this page helpful?