Does onCompleted works with useMutation?
Asked Answered
C

2

6

I am using useMutation hook in react project. The mutation runs successfully but it's not reaching onCompleted afterwards.

I have set notifyOnNetworkStatusChange to true in the mutation but that doesn't seem to help.

const [createUser] = useMutation(SIGNUP_MUTATION);

createUser({
  variables: {
     firstname,
     lastname,
     email
  },
  notifyOnNetworkStatusChange: true,
  onCompleted: (data) => {
     // not called
     confirm(data);
  }
});
Caper answered 30/8, 2019 at 20:36 Comment(0)
E
22

Looking at the api of useMutation it seems like you're using onCompleted in the wrong place. it should be part of the useMutation definition.

  const [createUser] = useMutation(
    SIGNUP_MUTATION,
    {
      onCompleted(data) {
        confirm(data);
      }
    }
  );

  return (
    <div>
      <form
        onSubmit={e => {
          e.preventDefault();
          createUser({ variables: { firstname, lastname, email } }); // assuming `firstname`, `lastname` and `email` are defined somewhere.
        }}
      >
        <input type="text" />
        <button type="submit">Signup</button>
      </form>
    </div>
  );
Eon answered 30/8, 2019 at 22:56 Comment(0)
B
-1

A couple years later -- we can now to do this:

const [saveFormData] = useMutation(SAVE_FORM_DATA_MUTATION);

saveFormData({
    variables: {
        myVariable: 'test variable'
    },
    update: (cache, data) => {
        //do updates here
    }
})
Bedabble answered 13/9, 2021 at 20:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.