in React JS by
Q:

How to update a component every second?

1 Answer

0 votes
by

You need to use setInterval() to trigger the change, but you also need to clear the timer when the component unmounts to prevent errors and memory leaks.

componentDidMount() {
  this.interval = setInterval(() => this.setState({ time: Date.now() }), 1000)
}

componentWillUnmount() {
  clearInterval(this.interval)
}
Click here to read more about React JS
Click here to read more about Insurance

Related questions

0 votes
asked Mar 2, 2020 in React JS by RShastri
0 votes
asked May 20, 2020 in React JS by SakshiSharma
0 votes
asked Mar 1, 2020 in React JS by RShastri
0 votes
asked Jun 5, 2020 in PouchDB by AdilsonLima
0 votes
asked May 21, 2019 in Mathematics by sheetalkhandelwal
...