run code when useQuery updates
Hi everyone! Is there any way to run some callback when data from useQuery is updated, for example showing some toast in react app?
2 Replies
In react you can run a callback when a hook's result changes by calling useEffect
https://react.dev/reference/react/useEffect
useEffect – React
The library for web and native user interfaces
For some background see this discussion of why they are react-query (a different library) removed their onSettled/onSuccess/onError callbacks. They do implement global query callbacks which have considered. https://tkdodo.eu/blog/breaking-react-querys-api-on-purpose
Breaking React Query's API on purpose
Why good API design matters, even if it means breaking existing APIs in the face of resistance.