-
Notifications
You must be signed in to change notification settings - Fork 2.7k
onComplete
is invoked after rendering with loaded data in 3.8.x
#11209
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
Comments
3.8 includes a change that is generally recommended by the React team when dealing with async data sources (like Apollo Client): It calls the Generally, please note that we don't have a lot of control over the order of operation here - React decides pretty arbitrarily when it rerenders. Once React fixes this bug the timing will likely go back to the behavior you have seen in React 18 with 3.7. => We have never been able to guarantee a certain order of operation, and can also not do so going into the future. React decides when it actually rerenders, not us. Out of curiosity - what are you doing here that needs to happen before a render? |
@phryneas Thank you for quick response!
We are building/committing/uncommitting/resetting our internal models based on query data within |
Hm, if that is local component state, most of the time you should be able to do that synchronously during component render - can you share some example code here? Maybe I can suggest an alternative way of doing so. |
Very simplified approach looks like: const MyComponent = ({ model }) => {
const { loading } = useSomeDataQuery({
onCompleted(data) {
model.setData(data);
model.commit();
}
});
return loading ?
<Loader/> :
<ModelView={ model }/>;
}; I could get the same behaviour with const MyComponent = ({ model }) => {
const { data, loading } = useSomeDataQuery({});
const modelWithData = React.useMemo(
() => {
if(loading) {
return null;
}
model.setData(data);
model.commit();
return model;
},
[loading, data]
);
return loading ?
<Loader/> :
<ModelView={ modelWithData }/>;
}; |
The same issue |
Yeah, im having this issue as well. Apollo client v3.8.8, react v18.2, nextjs v14. After a mutation, refetchQueries runs, query is updated, data is returned but onCompleted isn't called so the component state doesn't update. Previously working on 3.7 |
Same, updated to Apollo v3.8.6 and this started happening. However I agree with the new behavior. If the onComplete handler took minutes to run, it shouldn't block the component from getting the data. |
Issue Description
Something has been broken in 3.8.x versions.
onCompleted
now is invoked after rendering with loaded data, it's unexpected and leads to errors in our application because we need to do some additional work with loaded data before it's rendered. In 3.7.17 version this works as expected.Link to Reproduction
https://codesandbox.io/s/sad-fermat-djg7mq
Reproduction Steps
render true
completed
render false
@apollo/client
to3.8.3
, saverender true
render false
completed
The text was updated successfully, but these errors were encountered: