在setState中的回調函數中拿到react
this.setState({ count: this.state.count + 1 },()=>{ console.log(this.state.count)})
在setTimeOut中拿到dom
setTimeout(()=>{ this.setState({count:this.state.count}) console.log(this.state.count) },0)
在原生事件中修改狀態異步
state = { count:0 }; componentDidMount() { document.body.addEventListener('click', this.changeVal, false); } changeVal = () => { this.setState({ number: 1 }) console.log(this.state.count) }