使用class定义组件的时候,首先要先从react中解构出React.Component,并继承它 class Test extends Component{ //继承Component,Component相当于React.Component render(){ return
1、首先找到组件对应的类,并new了这个类的一个实例 2、通过实例找到原型上的render函数,让render执行 3、ReactDOM.render接收到原型上render函数retrun的虚拟的dom 4、将虚拟dom转换成真实dom,插入到页面中
1.this.setState是异步的 在你调用了this.setState后在他的下面输出他的结果还是没变的状态 this.setState({count:this.state.count+1}) console.log(this.state,'000') //结果式之前的,不是最新的 2.this.setState的第一个参数可以是一个对象,也可以是一个函数返回一个对象, 函数的参数是上一次的state this.setState((prevState)=>({count:prevState.count+1})) 3.this.setState的第二个参数是它的回调函数,在前面重新给state赋值后执行 this.setState({count:this.state.count+1},()=>{ console.log(this.state.count) })
setTimeout(()=>{
this.setState({count:this.state.count+1})
this.setState({count:this.state.count+1})
this.setState({count:this.state.count+1})
},100)