zoukankan      html  css  js  c++  java
  • react面试题——理解setState(源码object.assign)

    setState是异步的方式
    this.setState({
        counter:this.state.counter+1
    })
    console.log(this.state.counter)
    setState是异步,执行最后一个setState
    同步执行的方法,传递函数
    1,this.setState(nextState=>{
        return{
           counter: nextState.counter+1
        }
    })
    2,setTimeout(()=>{
        this.changeValue()
    },1000)
    3,
    this.setState({
        counter:this.state.counter+1
    },()=>{
        console.log(2)
    })
    4,原生事件
     doccument.body.addEventListener('click',this.changeValue,false)
    注释
    setState只有在合成事件的和生命周期才是异步,原生事件好的setTimeout是同步的,这里的异步的是批量更新
  • 相关阅读:
    网络规划和布线
    网络编程
    NoSQL 非关系数据库
    OpenCV
    首页
    C++关键字
    TCP/IP 详解7 Ping指令
    TCP详解 (1)
    GRE封装解封装过程
    GRE tunnel
  • 原文地址:https://www.cnblogs.com/yayaxuping/p/11972346.html
Copyright © 2011-2022 走看看