我们说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;
添加回答
举报
0/150
提交
取消