我是 react js 的新手,目前我正在尝试遵循官方 reactjs 文档,运行一些示例。最近我尝试了这个例子:https : //reactjs.org/docs/state-and-lifecycle.html
但是当我把这段代码:
import React from 'react';
import ReactDOM from 'react-dom';
class Clock extends React.Component {
state = {
date: new Date()
};
constructor(props:any) {
super(props);
this.timerID = 0;
}
componentDidMount() {
this.timerID = setInterval(
() => this.tick(),
1000
);
}
componentWillUnmount() {
clearInterval(this.timerID);
}
tick() {
this.setState({
date: new Date()
});
}
render() {
return (
<div>
<h1>Hello, world!</h1>
<h2>It is {this.state.date.toLocaleTimeString()}.</h2>
</div>
);
}
}
ReactDOM.render(
<Clock />,
document.getElementById('root')
);
Run Code Online (Sandbox Code Playgroud)
但是在 …