变更物件状态变更

时间:2018-09-17 17:55:44

标签: javascript reactjs apollo react-apollo

<Mutation mutation={addUserQuery} variables={{
  username: "AuraDivitiae",
  firstname: "Michael",
  lastname: "Lee"
}}>
  {
    (addUser, data) => {
      count = count + 1
      console.log("render no:" + count);
      console.log(data)

      return (
        <div className="form">
          <form onSubmit={(e) => {
            e.preventDefault();
            new Promise((resolve, reject) => { addUser(); resolve("Mutation function called!") }).
              then((prevData) => { console.log(prevData); console.log(data); })
          }}>
            <button type="submit">Add User</button>
          </form>
        </div>
      );
    }

  }
</Mutation>

控制台日志

    render no:1
    main.js:88 
{called: false, loading: false, data: undefined, error: undefined, client: DefaultClient}

----------------单击的按钮-------------------------- ----------------

    render no:2
    main.js:88 
{called: true, loading: true, data: undefined, error: undefined, client: DefaultClient}

    main.js:98 
Mutation function called!

    main.js:98 
{called: false, loading: false, data: undefined, error: undefined, client: DefaultClient}

    render no:3
    main.js:88 
{called: true, loading: false, data: {…}, error: undefined, client: DefaultClient}

从这些日志中,我可以看到最初在装载Mutate组件时,我们得到的渲染编号为:1。 在上单击该按钮时,将在promise中调用addUser()mutate函数,并通过“调用了Mutation函数”来解析。 但是,在日志中,它在main.js:88处显示了mutate结果对象,这意味着其重新呈现了组件。

我的问题是,调用mutate函数是否会更新重新渲染组件的状态?控件是否传递给了mutate函数,以便在可以解决承诺并随后使用它之前重新渲染组件? 但是重新渲染后,数据显示不应该调用吗?

0 个答案:

没有答案