import React from 'react';
import ReactDom from 'react-dom';
let number = 0;
class Clock extends React.Component {
constructor (props) {
super (props);
this.state = {
num: 0
}
}
componentDidMount() { // 生命周期
this.timerID = setInterval(
() => {
number = number +1
this.tick(number);
},
1000
);
}
componentWillUnmount() { // 生命周期
clearInterval(this.timerID);
}
tick(number) {
this.setState({
num: number
});
}
render () {
return (
<div>
<h1>Hello, world!</h1>
<h2>现在是 {this.state.num}</h2>
</div>
);
}
}
ReactDom.render(
<div>
<Clock></Clock>
</div>,
document.getElementById('root')
)
React—计时器(每秒数字增加1)
猜你喜欢
转载自blog.csdn.net/Poppy_LYT/article/details/100702071
今日推荐
周排行