Skip to content
Discussion options

You must be logged in to vote

The problem is the onSuccess callback doesn't trigger when query has staleTime, thus not setting my initialState.
Any way to avoid this problem (without using useEffect) ?

my suggestion would be to keep server and client state separate, thus not syncing the initial state to the local state. But its usually a bigger refactoring if you do it that way already.

useEffect is indeed the preferred solution for the problem. onSuccess is tied to a network request, just like onError. data returned from useQuery is referentially stable, so all you need is:

useEffect(() => {
  if (data) {
    dispatch(...)
  }
}, [data]

we've discussed this to some extent:

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jainishan2323
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants