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() { let date = this.state.date; let year = date.getFullYear(); let month = date.getMonth(); let day = date.getDay(); let seconds = date.getSeconds(); return (
{year}年は{month}月の{day}日 {seconds}秒
); } } /* ReactDOM.render( , document.getElementById('clock') );*/