Re-running a query with different payloads/ variables #2811
Replies: 1 comment 3 replies
-
react is pretty good at batching state updates from event callbacks already, and it will also get better in react 18 for async callbacks. If you have multiple pieces of state that update together, they should probably live together in the same state object. If you have different parts of your ui that update different parts of your state, then I have a blog post on that topic here: https://tkdodo.eu/blog/use-state-vs-use-reducer
How does the user update the page number and some filters at the same time? Correct me if I'm wrong, but it sounds to me like you have some kind of filter form where the user can do some changes and then trigger a refetch when they click A use case like that works very well if you move the State from the form up once apply is clicked, and then have the query only depend on that state. I personally like to put filters like that into the url and then subscribe to the url with It's basically the same as saying: "please refetch with this data when I click that button", but in a declarative way. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone,
So on my current project I created a small hook something similar to this library. However, for my next project I'm wanting to replace my hook for "react-query" as it has most if not all the functionality I need.
So I can see useQuery have a "re-fetch" function, however I can see this will just re-run the query. What I want to achieve is to be able to re-run the query with a different payload.
I know if you change some state and it's a depedency of a query it will re-run, but if you update multiple pieces of state like page number and filters that will probably re-run the query twice which isn't ideal.
What I want to be able to do is pass some initial pieces of state as part of the queryFn for useQuery (This state won't be updated, it's just the initial payload for the request), but then as I interact with the ui I want to update some state like page numbers and filters and then manually re-run the query with a new payload.
Is what I want possible ? I couldn't find anything in the docs like this hence why I've written this post.
Beta Was this translation helpful? Give feedback.
All reactions