I am using react-query, and there is a useCallback function dependent on some data from useQuery, but when I call useQuery's refetch funciton, the data in useCallback not update. I have done a test in useEffect, and the data in useEffect has updated.
useCallback
const updateCurrentActiveTemplate = useCallback(
(type: string) => {
console.log(templatesListQuery.data);
},
[templatesListQuery.data]
);
useEffect
useEffect(() => {
console.log(templatesListQuery.data, 'effect');
}, [templatesListQuery.data]);
the refetch part
const handleTemplateDataSubmit = () => {
await templatesListQuery.refetch();
updateCurrentActiveTemplate(currentBusinessType);
}
When I click the submit button I will call the handleTemplateDataSubmit method.
When I call useQuery's refetch function, the two logs are all executed, but the data in useCallback and useEffect are different, data in useCallback is the stale data and data in useEffect is updated, why is that.