-
Notifications
You must be signed in to change notification settings - Fork 786
Fix infinite loop caused by set state in onError / onCompleted props of Query #2751
Fix infinite loop caused by set state in onError / onCompleted props of Query #2751
Conversation
@chenesan: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Meteor Contributor Agreement here: https://contribute.meteor.com/ |
@hwillson the "github account" field in MCA website seems broken, I didnt' find any button or input to make me sign in github. |
Thanks for the heads up @chenesan - we'll take a look. |
@chenesan The CLA app should be working again. Thanks! |
@hwillson thanks, that works! |
The fix is done and waiting for review :-) |
Hello @peggyrayzis :) This PR is sitting ready for over 3 weeks now without any sort of response from the apollographql team. I feel like this both unfair to the community and the contributor @chenesan. I figured, as the Engineering Manager of apollographql you might want to take a look and improve the situation :) Thank you very much. |
any news on the code review? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks very much for working on this @chenesan!
I'd like to get some feedback on these changes before merging them. If anyone is interested in helping test these changes out, please install |
@hwillson When will this be released? I have react-apollo v2.5.2 and this fix doesn't seem to be present. Really needed as I'm also coming across onError infinite loops |
@shadrech This has been released in 2.5.3 |
Is this fix in @apollo/react-hooks? I get this issue with 3.0.0 |
It seems like there is no issue with @apollo/react-hooks: https://codesandbox.io/s/goofy-robinson-rfyjr |
@adrienharnay Ah yes but I was talking about in useQuery: fetchPolicy: "cache-and-network",
onCompleted: () => {
setCount(prevCount => prevCount + 1);
} Maybe it's an anti-pattern. I ended up resolving it by using React's |
Ah, indeed. Can confirm the bug is still occuring, but... Is it even a bug? Is it not normal that, when a Query/useQuery is re-rendered, the query is performed again (thus, changing state in onCompleted would result in an infinite loop, just like changing state in useEffect, but as you said you can guard against this by using useMemo)? It seems counter-intuitive to me for Apollo to handle this. A user might wonder why onCompleted/onError is called once, but not called on subsequent queries. |
But it doesn't follow the same behavior as the Query component. I'm migrating to hooks right now, and I'm having to deal with the issue of onCompleted being called every render, where it wasn't an issue before. I always saw onCompleted being something that would only be called when new data is fetched. Also, as pointed out here #3353 (comment), passing an empty variables object in the useQuery options stops this behavior, which makes it seem more like a bug. |
Fix #2477 .
The infinite loop is because in #2190 we try to handle onError/onCompleted for local cache so we move the handler logic from
updateCurrentData
tocomponentDidUpdate
. Once the upper component update its state in onError/onCompleted handler, it will causeQuery
update and incomponentDidUpdate
it would call onError/onCompleted again, so the infinite loop happens.In this PR we move the onError/onCompleted handle logic back to
updateCurrentData
, and incomponentDidUpdate
check if thequery
andvariables
are as same as prevProps (I'm not sure if it's safe, or we should consider more other props here). if it's different then we check if the currentResult is loaded / error (cached) to call handler. If the update is caused by fetching success/error, the props will be the same so it will not call onError/onComplete incomponentDidUpdate
.