changes to onSuccess callback #2964
Send to my agentClosed
TkDodo opened this issue ·3 comments
Discussed in https://github.com/tannerlinsley/react-query/discussions/2911#
Originally posted by **phatmann** November 11, 2021
Calling `QueryClient.setQueryData` method triggers the `onSuccess` callback. For many users, including me, this is unexpected behavior, and can be the source of recursion issues, since the `onSuccess` handler might make calls to `setQueryData`.
I propose we create a new onDataChanged callback. This would be trigged when cache data is changed via setQueryData or via a fetch. The onSuccess callback would only be called after a successful fetch.
For completeness, the same onDataChanged callback would be added to QueryCache.
TkDodo added labelsv4
TkDodo changed the title
talked with @tannerlinsley about this today, and we settled on the following:
onSuccessshould only be called when a fetch is made, basically tying it to the promise. This is what most people expect and why this change is breaking.- we're not going to introduce another callback. The observers will be informed anyways when data changes, and it would very likely have to be a side-effect during render in the observer in cases where we never even fire request because we read from the cache, which means we would need to implement it with useEffect in useBaseQuery anyhow. We're going to communicate that the best way to trigger a side-effect is, in fact, a useEffect in user-land. Since
datareturned fromuseQueryis stable and structurally shared, you'd just need:
const { data } = useQuery({ queryKey, queryFn }) useEffect(() => mySideEffectHere(data), [data])This 100% makes sense. Great call.
TkDodo assignedTkDodo
TkDodo closed this
Sign in to join this conversation.
@phatmann let me know if you want to work on this, too :)