Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does onCompleted works with useMutation?

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);
  }
});
like image 883
Sibgha Samdani Avatar asked Aug 30 '19 20:08

Sibgha Samdani


1 Answers

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>
  );
like image 153
talal7860 Avatar answered Nov 23 '22 17:11

talal7860