-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Still no alternative to onCompleted change in 3.8 #12056
Comments
Hey @flashtheman 👋 To clarify, are you saying that after upgrading to > 3.8, As for the bad news. At this time we aren't planning to introduce anymore changes to Would you be able to provide some context for how you're using I'm sorry I don't have a better answer for you at this time otherwise. |
Hi @jerelmiller, Thank you for your detailed response. I'll look into creating a runnable reproduction if that would help. Here’s my specific situation:
I realize you may have thought I was referring to the refetch method returned by useQuery, but I was actually referring to the refetchQueries option from a separate mutation. |
That helps! Appreciate the context! This is one of those cases where it sort of worked by accident in 3.7.x or before. Using that object syntax with The That said, I'm curious, what kind of logic are you executing in |
We primarily use the onCompleted function to update a state variable. This state variable is either used to render something in the current component or passed down as a prop to a child component. That’s the main use case. However, starting from version 3.8.x, the state variable no longer updates as expected (because onCompleted is not executed anymore), which, based on your explanation, makes sense. From what I understand, a possible solution is to directly use the data object from the useQuery hook and then update the state variable inside a useEffect hook that watches for changes in the data object. |
Is the state variable you're setting based on the data returned from the query? If so, we'd recommend using a derived value instead: // Don't do this:
const [count, setCount] = useState(0);
const { data } = useQuery(query, {
onCompleted: (data) => setCount(data.items.length)
});
// instead do this:
const { data } = useQuery(query);
const count = data.items.length; I don't know if this is how you're using it, but if so, use a derived value which will be much more accurate since it will be kept in sync with |
Issue Description
In Apollo Client 3.8, the behavior changed such that onCompleted is no longer called when another component triggers a refetch of the corresponding query. In versions <= 3.7.x, onCompleted was always invoked when notifyOnNetworkStatusChange was set to true.
We have now been waiting for a year for an alternative to the old behavior, as our app heavily relies on this functionality.
Is there any plan to provide an alternative, or is there an established workaround? The only solution we know of is using useEffect to watch the data object, but this would require significant changes to our application. Because of that, we've been hoping for an official solution to this issue.
Link to Reproduction
none
Reproduction Steps
No response
@apollo/client
version3.8.x
The text was updated successfully, but these errors were encountered: