我們說setState是批量更新但是下面代碼顯示的star是3有誰知道為什么嗎?import React, { Component } from 'react';class App extends Component { constructor(props) { super(props); this.timer = null; this.state = { star: 0 }; } componentDidMount() { this.timer = setTimeout(() => { this.setState({ num: this.state.star + 1 }); this.setState({ num: this.state.star + 1 }); this.setState({ num: this.state.star + 1 }); }, 5000); } render() { return <div>{this.state.star}</div>; } componentWillUnmount() { clearTimeout(this.timer); }}export default App;
React setState 不符合直覺
慕妹3146593
2019-03-20 17:19:14