How Reeact Updates Template / Component

React updates the component as long as you call setState, even if nothing has changed and even if the state is not used in the template

by darkylmnx

HTML

<div id="app"></div>

React

class App extends React.Component {
	constructor(props) {
  	super(props)
    
    this.state = {
    	nb: 0
    }
  }
  
  componentDidUpdate() {
  	alert('You will always see me as long as there is a setState wether or not the state is used in the template')
  }
  
  render() {
  	return (
      <div>
        <p>hello { this.state.nb }</p>
        <p>
          <button onClick={() => this.setState({nb: this.state.nb + 1})}>
            force re-render
          </button>
        </p>
      </div>
    )
  }
}

ReactDOM.render(
  <App />,
  document.getElementById('app')
);