Query on click #3423
Replies: 2 comments 5 replies
-
Hi, I see react-query suitable for calls which you want to trigger by state change. In my experience most of scenarios but not all of them. This is your case. with refetch const MyComponent () => {
[someStateData] = useState([]); // modified by the user interacting with the page
const { refetch } = useQuery(/* query using someStateData */);
const save = async () => {
await refetch();
// your mutations
});
return <button onClick={() => save(true)} >Save</button>
} Alternativly withou react-query const MyComponent () => {
[someStateData] = useState([]); // modified by the user interacting with the page
const save = async () => {
await myComputation(someStateData);
// your mutations
});
return <button onClick={() => save(true)} >Save</button>
} Hope it helps. :) |
Beta Was this translation helpful? Give feedback.
-
If your query depends on
https://react-query.tanstack.com/guides/query-functions#query-function-variables https://tkdodo.eu/blog/practical-react-query#treat-the-query-key-like-a-dependency-array |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a page with some states the user can change.
Once changes are made, he can save them.
To actually save the state, I need to query my API first, makes some computation, then perform a series of mutations.
Since I cannot conditionally query, I used the following pattern :
The problem is that I have no guarantee about the execution here, the user can click
Save
, then immediately change the state, triggering a new query before the previous save is done, triggering the effect again.Is there any recommended pattern for this kind of scenarios ?
Beta Was this translation helpful? Give feedback.
All reactions