arthur
arthur13mo ago

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
lee
lee13mo ago
In react you can run a callback when a hook's result changes by calling useEffect
const result = useQuery(...); // or useState or another hook
useEffect(() => {
displayToast(result);
}, [result]);
const result = useQuery(...); // or useState or another hook
useEffect(() => {
displayToast(result);
}, [result]);
https://react.dev/reference/react/useEffect
useEffect – React
The library for web and native user interfaces
ballingt
ballingt13mo ago
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.

Did you find this page helpful?