1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| import React, { Component } from 'react'
class Demo extends Component { state = { count:0 } update1 = () => { console.log('update1 更新前',this.state.count) this.setState(state=>({count:state.count+1})) console.log('update1 更新后',this.state.count) } componentDidMount(){ console.log('componentDidMount 更新前',this.state.count) this.setState(state=>({count:state.count+1})) console.log('componentDidMount 更新后',this.state.count) } update2 = () => { setTimeout(()=>{ console.log('定时器 更新前',this.state.count) this.setState(state=>({count:state.count+1})) console.log('定时器 更新后',this.state.count) },3000) } update3 = () => { let count_dom = this.refs.count_dom count_dom.onclick = () => { console.log('原生DOM事件监听回调 更新前',this.state.count) this.setState(state=>({count:state.count+1})) console.log('原生DOM事件监听回调 更新后',this.state.count) } } update4 = () => { Promise.resolve().then(value=>{ console.log('promise 更新前',this.state.count) this.setState(state=>({count:state.count+1})) console.log('promise 更新后',this.state.count) }) } render() { const {count} = this.state console.log('render渲染',count) return ( <> <h2 ref="count_dom">{count}</h2> <button onClick={this.update1}>更新1</button> <button onClick={this.update2}>更新2</button> <button onClick={this.update3}>更新3</button> <button onClick={this.update4}>更新4</button> <button onClick={this.update5}>更新5</button> </> ); } } export default Demo;
|