react倒计时

export default class TimerTest extends Component{
  constructor(props) {
    super(props);
    this.state = {
      seconds:0
    };
  }

  tick(){
    this.setState(preState =>({
      seconds:preState.seconds+1
    }));
  }


  componentDidMount() {
    this.interval = setInterval(() => this.tick(), 1000);
  }

  componentWillUnmount() {
    clearInterval(this.interval);
  }

  render(){
    return(
      <div>
        Seconds:{this.state.seconds}
      </div>
    );
  }
}

  

posted @ 2018-06-15 10:13  爱吃醋的工程师  阅读(158)  评论(0编辑  收藏  举报