August 2017
Beginner
298 pages
7h 4m
English
Take a look at the Counter class we created before. As the name suggests, it should render a counter that increases by 1 every second. For that, we need to use setInterval to increase the count property of the counter's state object. We can use either the componentWillMount or componentDidMount lifecycle methods to add setInterval. Since this process does not need any reference to DOM elements, we can use componentWillMount.
Inside the Counter class, we need to add the following lines of code:
increaseCount() { this.setState({ count: this.state.count+1 }) }componentWillMount() { setInterval(this.increaseCount.bind(this), 1000); }
This will automatically perform the increment every second and the render method will update ...
Read now
Unlock full access