Not memoize onCompleted and onError never resolves the query
See original GitHub issueIntended outcome:
The following query should log either Completed
or Error
to the console.
useQuery(QUERY, {
fetchPolicy: 'network-only',
onCompleted: () => console.log('Completed'),
onError: () => console.log('Error'),
})
Actual outcome: The query is never resolved and the component re-renders infinitely.
Is it now recommended to make sure to memoize the query/mutation callbacks (onCompleted
, onError
, update
, etc) when used with fetchPolicy: 'network-only'
? The version, @apollo/client@3.0.0-beta.45
resolved the callbacks fine without having to memoize them.
How to reproduce the issue: I’ve created a reproduction on the Code Sandbox: https://codesandbox.io/s/sleepy-mendeleev-1q9jf?file=/src/App.js
Open up the console and you will see loading
being logged in the console infinitely. If you however comment the non-memoized onCompleted
and onError
and uncomment the memoized version and refresh the browser (as below), it will successfully resolve the query and show the data.
const { loading, data } = useQuery(ALL_PEOPLE, {
fetchPolicy: "network-only",
// onCompleted: () => console.log("Hello"),
// onError: () => console.log("error")
onCompleted: useCallback(() => console.log("Hello"), []),
onError: useCallback(() => console.log("error"), [])
});
Versions
Reproducible from @apollo/client: 3.0.0-beta.46
Issue Analytics
- State:
- Created 3 years ago
- Reactions:22
- Comments:35 (9 by maintainers)
Heads up: we’re not quite ready to publish
@apollo/client@3.1.0
yet, but I’ve published an@apollo/client@3.1.0-pre.0
release that includes #6588, if you want to try that in the meantime. 🙏@pcunha29 I put the project on hold, but I will look into it tomorrow and see if I can get it finished for end of next week. Sorry for the delay. I feel your pain and have completely moved away from Apollo.