我们说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;
慕田峪9158850
相关分类