代码之家  ›  专栏  ›  技术社区  ›  Kim Stacks

如何改进react 16.4中数组状态的慢更新

  •  0
  • Kim Stacks  · 技术社区  · 4 年前

    我使用的是react 16.4,我有一个setState,它占用了将近800毫秒,我不知道为什么。

          let shown_wbs = [...this.state.shown_wbs];
          console.log(shown_wbs);
    
          // and reset the line item value
          console.log("before set");
          let current = new Date().getTime();
          let difference = current - last_known;
          console.log(difference);
          last_known = current;
          shown_wbs.push("somestring");
          console.log("set state");
          current = new Date().getTime();
          difference = current - last_known;
          console.log(difference);
          last_known = current;
          this.setState({
            shown_wbs: shown_wbs,
          });
          console.log("after set state");
          current = new Date().getTime();
          difference = current - last_known;
          console.log(difference);
          last_known = current;
    

    本例中的数组只是一个字符串数组。

    如何提高设置状态的速度?

    0 回复  |  直到 4 年前