社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
我们最终做到的页面效果是这样的:
npm install -g create-react-app
create-react-app asia_timer
经过一系列的等待之后,我们的应用就创建好了。npm start
来启动这个示例程序。效果如下:var time = formatTime(this.state.time);
<h1 className="display-time">{time}</h1>
,我们可以看到,实际上展示的就是这个time,那这个元素我们怎么去获取呢? //在构造函数中对状态赋初始值
constructor(props){
super(props);
//不能调用state
this.state = {
time:0,//上部的时间值
on:false,//按钮开始和暂停的控制状态
log:[] //数组 用于下部的记录
};
}
我们的思路是,构造函数中定义了这个time,然后在用户按开始按钮事,去动态的改变这个time(做+1 和 格式化处理),这样,系统就会自动的调用render()方法,重新绘制整个页面,从而在绘制上部h1标签时,将最新的time绘制到界面上去。import React from 'react';
class Button extends React.Component{
//静态属性,给属性赋默认值
static defaultProps = {
onClick : null,
className : '',
text : '默认'
};
render(){
return <button className={`Button ${this.props.className}`} onClick={this.props.onClick}>{this.props.text}</button>;
}
}
export default Button;
import React from 'react';
import formatTime from '../utils/formatTime';
class DisplayLog extends React.Component{
//log数组长度等于0,返回“空空如也”
renderEmpty = () =>{
return <span className="empty-log">空空如也...</span>;
}
//否则,把元素遍历(时间),得到一系列的<li>
renderLog = () => {
//<li>00</li>
//<li>11</li>
return this.props.log.map(item => { //css3 的遍历方法
return <li className="log-item" >{formatTime(item)}</li>;
});
}
render(){
//log数组长度等于0,返回“空空如也”
//否则,把元素遍历(时间),得到一系列的<li>
//<ul>
// <li>00</li>
// <li></li>
//</ul>
const log = this.props.log.length === 0 ? this.renderEmpty() : this.renderLog();
return <ul className="log">
{log}
</ul>;
}
}
export default DisplayLog;
import React from 'react';
import DisplayLog from './DisplayLog';
import Button from './Button';
import formatTime from '../utils/formatTime';
class TimeDisplay extends React.Component{
//在构造函数中对状态赋初始值
constructor(props){
super(props);
//不能调用state
this.state = {
time:0,
on:false,
log:[] //数组
};
}
//开始按钮
handleToggle = ()=>{
//已经开始,取消正在运行的定时器
if(this.state.on){
clearInterval(this.timer);
}else{
//否则开启
//定时器,10ms累加1
this.timer = setInterval(()=>this.setState({time:this.state.time+1}),10);
}
this.setState({on:!this.state.on});
}
//记录时间
handleLogTime = ()=>{
//存储在数组中
this.state.log.push(this.state.time);
}
//清空记录
handleClearLog = ()=>{
this.setState({log:[]});
}
//重置
handleReset = () =>{
this.setState({time:0});
}
render(){
var time = formatTime(this.state.time);
return <div>
<h1 className="display-time">{time}</h1>
<div className="controls">
<Button className={this.state.on ? "danger" : "success"} text={this.state.on ? "暂停" : "开始"} onClick={this.handleToggle}/>
<Button className="warning" onClick={this.handleReset}/>
<Button className="primary" text="记录" onClick={this.handleLogTime}/>
<Button className="undefined" text="清空" onClick={this.handleClearLog}/>
</div>
<DisplayLog log={this.state.log}/>
</div>;
}
//监听键盘事件
componentDidMount(){
//内置对象
window.addEventListener('keydown', e => e.preventDefault());
window.addEventListener('keyup', e => {
e.preventDefault();
switch (e.keyCode){
case 32: //space
this.handleToggle();
break;
case 82://r R
this.handleReset();
break;
case 13:// Enter
this.handleLogTime();
break;
case 67://c C
this.handleClearLog();
break;
default:
break;
}
});
}
//组件被移除,事件监听取消
componentWillUnmount(){
window.removeEventListener('keydown');
window.removeEventListener('keyup');
}
}
//在外部要使用TimeDisplay
export default TimeDisplay;
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!