React-apollo: useQuery: onCompleted called on every update

Created on 12 Aug 2019  ·  5Comments  ·  Source: apollographql/react-apollo

Intended outcome:

Updating some state in onCompleted based on the result from a query.

Actual outcome:

Ending up in an endless loop, since every update to the component which calls the query, results in onCompleted getting called again.

How to reproduce the issue:

https://codesandbox.io/embed/apollo-client-error-template-78jbq
Pay attention to the console.

Version

@apollo/react-hooks 3.0.0

Most helpful comment

I noticed the same issue. This seems to be solved by passing variables: {} to the query options in the case where the query has no variables as in the example.

All 5 comments

Ah. I see where the issue lies.
The passed onCompleted callback is different on every render so it re-triggers.
Actually no re-fetch is triggered. Is this behavior expected?

I noticed the same issue. This seems to be solved by passing variables: {} to the query options in the case where the query has no variables as in the example.

@PovilasSlekys Thanks, you saved a few hours of my life!

@PovilasSlekys I don't know why but it's works!

This has been fixed by https://github.com/apollographql/react-apollo/pull/3419, and will be released shortly. Thanks!

Was this page helpful?
0 / 5 - 0 ratings