question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useMutation is not calling the latest version of onCompleted

See original GitHub issue

Intended 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 image

Versions

3.6.3

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
charle692commented, May 19, 2022

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 😃

3reactions
glenndixoncommented, May 16, 2022

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found