Replies: 9 comments
-
okay, what you are doing here has to do with the queries being First of all, What When you do step 5, you'll see that in the query cache, the data is still Hope that clears things up. I'd be interested in the use-case you are trying to solve with this approach :) |
Beta Was this translation helpful? Give feedback.
-
Thanks for the great answer! That makes sense. Would there be any downsides to having My use case is: I have two components:
The query itself is very expensive. So the user makes a bunch of tweaks to the control panel, hits run query (instead of fetching the query on every small tweak). Now I'd like the query results from that expensive query to stay in the results panel, while the user begins tweaking the controls for the next query. Is react query a bad option in this case? It definitely feels like I'm using |
Beta Was this translation helpful? Give feedback.
-
that's a cool use-case, but I don't see why this shouldn't work. Here's how I'd model this:
but where is the second observer coming into play that also needs that previous data, that I'm not quite getting with this example. |
Beta Was this translation helpful? Give feedback.
-
thanks @TkDodo ! More detail:
This is where I became surprised that the state wasn't shared across the observers (actually I didn't know about the observers, now I see what the number is in the react dev tools!) |
Beta Was this translation helpful? Give feedback.
-
okay so:
Does that sum it up alright? |
Beta Was this translation helpful? Give feedback.
-
Yes - slight change:
Maybe there's no practical difference between the |
Beta Was this translation helpful? Give feedback.
-
okay, why refetch imperatively and not drive the |
Beta Was this translation helpful? Give feedback.
-
If this is getting too specific to my app and more like advice, I'm happy to move to another channel!
Would that work if the reason it's disabled is that the I'm going to share a screenshot of the app. The user selects a bunch of metrics that they want to see, you can change filters, sort config. I've decided the UX is better (untested) if the results (table + chart) don't refresh if you change e.g. a sort. That's because the user usually makes a lot of changes together (change the sort, the filters, and add a column). So I want the chart and table to remain intact while the user starts to prepare the next query. |
Beta Was this translation helpful? Give feedback.
-
yep, I understand that. I usually solve that problem with a bit of state duplication, like have it in react state when you click around and do your filters, and then when you click apply / run query, I put the applied filters in the url. The useQuery hook listens to the url-state and the queryKey is built because of that, so it only changes and then runs immediately when the user clicks run query. On top of that, it gives me copyable urls as well. But your state could also be local and then lift-up-to-redux or so when you click Run query. I think because you don't have that and work with If you stick to that, I think you'd have to resort to but you would need to somehow find the previous one. It's probably the one that fuzzily matches your query key with the highest I'm gonna close this, as it's not an issue and will transfer to a discussion. We can continue talking there of course |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the bug
I might be using
useQuery
incorrectly: I assumed that usingkeepPreviousData: true
would mean that the any component using auseQuery
could never have undefined data once it's been fetched at least once. Since there would be data available from the first fetch.In the example below, I can mount a component that uses a
useQuery
with previous data but the results show as undefined.To Reproduce
Steps to reproduce the behavior:
Once the second component is mounted you can hit "++count" and "refetch" and they'll stay in sync until you unmount the second component again.
Expected behavior
I expected all components using the same
useQuery
to have access to the query in the same state without having to store theuseQuery
in a global context.Beta Was this translation helpful? Give feedback.
All reactions