首页 > 其他分享 >11 | setState 到底是同步的,还是异步的?

11 | setState 到底是同步的,还是异步的?

时间:2022-12-29 22:46:18浏览次数:29  
标签:11 异步 console log render count state setState

从一道面试题说起

import React from "react";
import "./styles.css";
export default class App extends React.Component{
  state = {
    count: 0
  }
  increment = () => {
    console.log('increment setState前的count', this.state.count)
    this.setState({
      count: this.state.count + 1
    });
    console.log('increment setState后的count', this.state.count)
  }
  triple = () => {
    console.log('triple setState前的count', this.state.count)
    this.setState({
      count: this.state.count + 1
    });
    this.setState({
      count: this.state.count + 1
    });
    this.setState({
      count: this.state.count + 1
    });
    console.log('triple setState后的count', this.state.count)
  }
  reduce = () => {
    setTimeout(() => {
      console.log('reduce setState前的count', this.state.count)
      this.setState({
        count: this.state.count - 1
      });
      console.log('reduce setState后的count', this.state.count)
    },0);
  }
  render(){
    return <div>
      <button onClick={this.increment}>点我增加</button>
      <button onClick={this.triple}>点我增加三倍</button>
      <button onClick={this.reduce}>点我减少</button>
    </div>
  }
}

控制台输出

异步的动机和原理——批量更新的艺术

一个完整的更新流程,涉及了包括 re-render(重渲染) 在内的多个步骤。re-render 本身涉及对 DOM 的操作,它会带来较大的性能开销。这正是 setState 异步的一个重要的动机——避免频繁的 re-render每来一个 setState,就把它塞进一个队列里“攒起来”。等时机成熟,再把“攒起来”的 state 结果做合并,最后只针对最新的 state 值走一次更新流程。这个过程,叫作“批量更新”,注意:对于相同属性的设置,React 只会为其保留最后一次的更新。

test = () => {
  console.log('循环100次 setState前的count', this.state.count)  // 0
  for(let i=0;i<100;i++) {
    this.setState({
      count: this.state.count + 1
    })
  }
  console.log('循环100次 setState后的count', this.state.count) // 0
}

 

标签:11,异步,console,log,render,count,state,setState
From: https://www.cnblogs.com/superlizhao/p/17013722.html

相关文章

  • 刷题笔记——1112:C语言考试练习题_一元二次方程
    题目1112:C语言考试练习题_一元二次方程代码importmathwhileTrue: try: a,b,c=map(float,input().strip().split()) delta=b*b-4*a*c x1=(-b+......
  • day11-功能实现10
    家居网购项目实现010以下皆为部分代码,详见https://github.com/liyuelian/furniture_mall.git24.bugFix-添加购物车按钮动态处理24.1需求分析/图解如某个家居的库存......
  • C++11:for循环(基于范围的循环)
    1.C++98/03标准的for循环在C++98/03标准中,如果要用for循环语句遍历一个数组或者容器,只能套用如下结构:for(表达式1;表达式2;表达式3){//循环体}例如,下面......
  • day11-分类和static
    1.案例驱动模式1.1案例驱动模式概述(理解)通过我们已掌握的知识点,先实现一个案例,然后找出这个案例中,存在的一些问题,在通过新知识点解决问题1.2案例驱动模式的好处......
  • java11 最新配置环境变量步骤
    1、首先按下快捷键“win+r”打开运行,输入cmd。  2、然后输入:SETJAVA_HOME=C:\ProgramFiles\Java\jdk-11.0.6  3、然后继续输入:SETCLASSPATH=%JAVA_HOME%\lib......
  • 几个函数的使用例子:更新VBRK-XBLNR,IB01设备BOM创建,LI11N输入库存盘点
    最近用到一些函数,网上的相关资料不多,这里记录一下。本文链接:https://www.cnblogs.com/hhelibeb/p/17012303.html 1,使用RV_INVOICE_HEAD_MAINTAIN更新VBRK-ZUNOR和VBR......
  • CodeForces 1163D Mysterious Code
    洛谷传送门CF传送门zxx的题单来的(发一个无脑kmp自动机+dp做法。看到题就很dp,考虑设计状态。显然填字母时要知道当前串与\(s,t\)的匹配位数,否则就不知道\(s,......
  • BM11 链表相加(二)
    题目描述思路分析之前做过两数相加,与这道题类似,但是那道题的相加顺序是排好的,比如:1000+20两个链表的排序都是从最低位开始的0->0->0->1,0->2,此时我们直接相加就可......
  • 青少年CTF练习11
    T11 CheckMe06题目难度:★题目描述:这竟然让我登陆?靶机下面有个txt,打开后发现里面有一大批登入的密码和账号,打开靶机     使用Burpsuite抓包爆破即可! ......
  • 软件需求设计方法学全程实例剖析幻灯片03-业务建模[2020-11更新]
    pdf文件下载:http://umlchina.com/training/umlchina_03_bm.pdf......