用state跟踪组件状态

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';

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() {
    return (
      

Hello,world

It is {this.state.date.toLocaleTimeString()}.

) } } ReactDOM.render( , document.getElementById('root') ); registerServiceWorker();

你可能感兴趣的:(用state跟踪组件状态)