State & 生命周期
作者:
ShinPlus | 来源:发表于
2019-07-23 09:54 被阅读0次class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {date: new Date()};
}
componentDidMount() {
this.timerID = setInterval(
() => this.tick(),
1000
);
}
componentWillUnmount() {
clearInterval(this.timerID);
}
tick() {
this.setState({
date: new Date()
});
}
render() {
return (
<div>
<h1>Hello, Rails!</h1>
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
</div>
);
}
}
ReactDOM.render(
<Clock />,
document.getElementById('root')
);
本文标题:State & 生命周期
本文链接:https://www.haomeiwen.com/subject/wbgnlctx.html
网友评论