6

我在反应项目中使用 useMutation 钩子。突变成功运行,但之后没有达到 onCompleted 。

我在突变中将 notifyOnNetworkStatusChange 设置为 true ,但这似乎没有帮助。

const [createUser] = useMutation(SIGNUP_MUTATION);

createUser({
  variables: {
     firstname,
     lastname,
     email
  },
  notifyOnNetworkStatusChange: true,
  onCompleted: (data) => {
     // not called
     confirm(data);
  }
});
4

2 回答 2

20

查看useMutation的 api,您似乎onCompleted在错误的地方使用。它应该是useMutation定义的一部分。

  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>
  );
于 2019-08-30T22:56:51.280 回答
-1

几年后——我们现在可以这样做:

const [saveFormData] = useMutation(SAVE_FORM_DATA_MUTATION);

saveFormData({
    variables: {
        myVariable: 'test variable'
    },
    update: (cache, data) => {
        //do updates here
    }
})
于 2021-09-13T20:56:51.247 回答