API
setState
  • Enqueues changes to the component state and tells React that this component and its children need to be re-rendered with the updated state
  • forceUpdate
  • Force a re-render of the component and its children in question. It does not mutate the state at all
  • import React from 'react';
    import ReactDOM from 'react-dom';
    
    class App extends React.Component {
       constructor() {
          super();
    
          this.state = {
             data: []
          }
    
          this.setStateHandler = this.setStateHandler.bind(this);
          this.forceUpdateHandler = this.forceUpdateHandler.bind(this);
       };
    
       setStateHandler() {
          var item = "setState..."
          var myArray = this.state.data.slice();
    	  myArray.push(item);
          this.setState({data: myArray})
       };
    
       forceUpdateHandler() {
    	   this.state.data = [];
          this.forceUpdate();
       };
    
       render() {
          return (
             <div>
                <button onClick = {this.setStateHandler}>setState</button>
                <button onClick = {this.forceUpdateHandler}>foreceUpdate</button>
                <h4>State Array: {this.state.data}</h4>
             </div>
          );
       }
    }
    
    ReactDOM.render(<App />, document.getElementById('root'));