useMutation is not calling the latest version of onCompleted
See original GitHub issueIntended outcome:
I set an onCompleted
callback funcA
for useMutation
, funcA
depends on state
, when I fetch and modify the state, onCompleted
still calls the old version of funcA
.
Actual outcome:
How to reproduce the issue:
https://codesandbox.io/s/usemutation-example-ke17zm?file=/src/index.jsx
click the add person button, expected to log after click
in console but before click
Versions
3.6.3
Issue Analytics
- State:
- Created a year ago
- Comments:8 (1 by maintainers)
Top Results From Across the Web
Mutations in Apollo Client - Apollo GraphQL Docs
To execute a mutation, you first call useMutation within a React component and pass it the mutation you want to execute, like so:...
Read more >Does onCompleted works with useMutation? - Stack Overflow
The mutation runs successfully but it's not reaching onCompleted afterwards. I have set notifyOnNetworkStatusChange to true in the mutation but ...
Read more >useMutation | Relay
API reference for useMutation, a React hook used to execute a GraphQL mutation.
Read more >Apollo useMutation React hook - Hasura
We will use the Apollo Client useMutation from @apollo/client in React app ... the onCompleted option which will be called once the mutation...
Read more >Should I Directly Access Data From the Apollo Client or From ...
const [logIn, { error }] = useMutation<LogInData, LogInVariables>(LOG_IN, { onCompleted: data => { if (data && data.logIn) { setUser(data.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Alright, fixed the issue in the linked ^ PR. I’ve confirmed in my application that is does indeed fix the issue but if you all have the chance to try it out on your end that would be appreciated 😃
@nathanmarks Check out this: https://github.com/apollographql/apollo-client/blob/main/src/react/hooks/useQuery.ts#L280
I haven’t tested but my interpretation based on the comment and code there is that useQuery DOES use the “latest” version of the options i.e. it stores the options in a ref which updates on every render.
So I think there IS an inconsistency here between useMutation and useQuery, which suggests to me some remediation is called for.