Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Fix infinite loop caused by set state in onError / onCompleted props of Query #2751

Merged
merged 11 commits into from
Mar 10, 2019

Conversation

chenesan
Copy link
Contributor

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 to componentDidUpdate. Once the upper component update its state in onError/onCompleted handler, it will cause Query update and in componentDidUpdate 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 in componentDidUpdate check if the query and variables 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 in componentDidUpdate.

@apollo-cla
Copy link

@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/

@chenesan
Copy link
Contributor Author

@hwillson the "github account" field in MCA website seems broken, I didnt' find any button or input to make me sign in github.

@hwillson
Copy link
Member

Thanks for the heads up @chenesan - we'll take a look.

@hwillson
Copy link
Member

@chenesan The CLA app should be working again. Thanks!

@chenesan
Copy link
Contributor Author

@hwillson thanks, that works!

@stnwk
Copy link

stnwk commented Jan 24, 2019

Thank you for tackling this issue, would really love to see this PR merged 😍What's the current state? @hwillson @chenesan

@chenesan
Copy link
Contributor Author

The fix is done and waiting for review :-)

@chenesan
Copy link
Contributor Author

@benjamn @hwillson Is there anything I can do to make progress for this PR?

@stnwk
Copy link

stnwk commented Feb 11, 2019

@benjamn @hwillson friendly ping

@stnwk
Copy link

stnwk commented Feb 12, 2019

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.

@jortizsao
Copy link

any news on the code review?

@hwillson hwillson self-assigned this Feb 27, 2019
Copy link
Member

@hwillson hwillson left a 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!

@hwillson
Copy link
Member

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 react-apollo@next (2.5.2-rc.0) and let me know if everything looks like it's working properly. Thanks!

@hwillson hwillson merged commit b591d03 into apollographql:master Mar 10, 2019
@shadrech
Copy link

@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

@adrienharnay
Copy link

@shadrech This has been released in 2.5.3

@shadrech
Copy link

👍🏾 @adrienharnay

@sekoyo
Copy link

sekoyo commented Aug 14, 2019

Is this fix in @apollo/react-hooks? I get this issue with 3.0.0

@adrienharnay
Copy link

It seems like there is no issue with @apollo/react-hooks: https://codesandbox.io/s/goofy-robinson-rfyjr

@sekoyo
Copy link

sekoyo commented Aug 14, 2019

@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 useMemo to compute a value once data changed.

@adrienharnay
Copy link

adrienharnay commented Aug 14, 2019

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.

@kylehoover
Copy link

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.

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

Successfully merging this pull request may close these issues.

Inifinite calling of onError in query hoc
9 participants