react实例7-时钟
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>test01</title>
<script src="build/browser.min.js"></script>
<script src="build/react.js"></script>
<script src="build/react-dom.js"></script>
<script type="text/babel">
class Clock extends React.Component{
constructor(...args){
super(...args);
this.state={
h:0,
m:0,
s:0
}
var _this=this;
setInterval(function(){
_this.tick();
},1000);
}
componentDidMount(){
this.tick();
}
tick(){
var oDate=new Date();
this.setState({
h:oDate.getHours(),
m:oDate.getMinutes(),
s:oDate.getSeconds(),
});
}
render(){
return <div>
<span>{this.state.h}</span>:
<span>{this.state.m}</span>:
<span>{this.state.s}</span>
</div>
}
}
window.onload=function(){
ReactDOM.render(
<Clock/>,
document.getElementById('app'),function(){
console.log("渲染成功啦")
}
);
}
</script>
</head>
<body>
<div id="app"></div>
</body>
</html>