【问题标题】:Does onCompleted works with useMutation?onCompleted 是否与 useMutation 一起使用?
【发布时间】:2020-01-04 01:48:49
【问题描述】:

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

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

const [createUser] = useMutation(SIGNUP_MUTATION);

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

【问题讨论】:

    标签: react-apollo-hooks


    【解决方案1】:

    查看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>
      );
    

    【讨论】:

      【解决方案2】:

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

      const [saveFormData] = useMutation(SAVE_FORM_DATA_MUTATION);
      
      saveFormData({
          variables: {
              myVariable: 'test variable'
          },
          update: (cache, data) => {
              //do updates here
          }
      })
      

      【讨论】:

        猜你喜欢
        • 2021-12-25
        • 2020-04-26
        • 2021-01-26
        • 2012-04-13
        • 2018-12-15
        • 2012-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多