Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes to onSuccess callback #2964

Closed
TkDodo opened this issue Nov 17, 2021 Discussed in #2911 · 3 comments · Fixed by #2969
Closed

changes to onSuccess callback #2964

TkDodo opened this issue Nov 17, 2021 Discussed in #2911 · 3 comments · Fixed by #2969
Assignees
Labels
Milestone

Comments

@TkDodo
Copy link
Collaborator

TkDodo commented Nov 17, 2021

Discussed in #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 TkDodo added the v4 label Nov 17, 2021
@TkDodo TkDodo added this to the v4 milestone Nov 17, 2021
@TkDodo
Copy link
Collaborator Author

TkDodo commented Nov 17, 2021

@phatmann let me know if you want to work on this, too :)

@TkDodo TkDodo changed the title onDataChanged callback changes to onSuccess callback Nov 17, 2021
@TkDodo
Copy link
Collaborator Author

TkDodo commented Nov 17, 2021

talked with @tannerlinsley about this today, and we settled on the following:

  • onSuccess should 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 data returned from useQuery is stable and structurally shared, you'd just need:
const { data } = useQuery({ queryKey, queryFn })
useEffect(() => mySideEffectHere(data), [data])

@phatmann
Copy link
Contributor

This 100% makes sense. Great call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants