简体   繁体   中英

How to use setInterval to update data and give it to react component?

Below is my function in AppHelper.js file

getTimeCountDown: function() {
  //some code...
  return  minutes + ":" + seconds;
}

And in my react component

<div>{AppHelper.getTimeCountDown()}</div>

and it will show like 23:00 on screen.

What I want to do, is call this function every 1 minutes, how can I do that?Can I use setInterval? where to use?

Think this is most wise decision:

componentDidMount(){
    this.interval = setInterval(() => {
      this.setState({time: AppHelper.getTimeCountDown()})
    }, 1000 * 60)
}
componentWillUnmount(){
   clearInterval(this.interval);
}
render() {
    return <div>{this.state.time}</div>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM