Easy Tutorial
❮ React Ref Render React Conditional Rendering ❯

React Component Lifecycle

In this section, we will discuss the lifecycle of React components.

The lifecycle of a component can be divided into three states:


Mounting

When a component instance is created and inserted into the DOM, the following lifecycle methods are called in order:

The render() method is the only one that must be implemented in a class component; other methods can be implemented as needed. Detailed explanations of these methods can be found in the official documentation.


Updating

A component updates whenever its state or props change.

The following lifecycle methods are called in order when a component's props or state change:

The render() method is the only one that must be implemented in a class component; other methods can be implemented as needed. Detailed explanations of these methods can be found in the official documentation.


Unmounting

The following method is called when a component is removed from the DOM:

Detailed explanations of these methods can be found in the official documentation.


Example

Here is an example of a current time display that updates every second:

class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = {date: new Date()};
  }

  componentDidMount() {
    this.timerID = setInterval(
      () => this.tick(),
      1000
    );
  }

  componentWillUnmount() {
    clearInterval(this.timerID);
  }

  tick() {
    this.setState({
      date: new Date()
    });
  }

  render() {
    return (
      <div>
        <h1>Hello, tutorialpro!</h1>
        <h2>The current time is: {this.state.date.toLocaleTimeString()}.</h2>
      </div>
    );
  }
}

ReactDOM.render(
  <Clock />,
  document.getElementById('root')
);

In the following example, after the Hello component is loaded, a timer is set up using the componentDidMount method to reset the component's opacity every 100 milliseconds and re-render it:

class Hello extends React.Component {

  constructor(props) {
      super(props);
      this.state = {opacity: 1.0};
  }

  componentDidMount() {
    this.timer = setInterval(() => {
      let opacity = this.state.opacity;
      opacity -= .05;
      if (opacity < 0.1) {
        opacity = 1.0;
      }
      this.setState({
        opacity: opacity
      });
    }, 100);
  }

  render() {
    return (
      &lt;div style={{opacity: this.state.opacity}}>
        Hello {this.props.name}
      </div>
    );
  }
}

ReactDOM.render(
  <Hello name="world"/>,
  document.getElementById('root')
);
if (opacity < 0.1) {
  opacity = 1.0;
}
this.setState({
  opacity: opacity
});
}.bind(this), 100);
}

render() {
return (
  &lt;div style={{opacity: this.state.opacity}}>
    Hello {this.props.name}
  </div>
);
}
}

ReactDOM.render(
<Hello name="world"/>,
document.body
);

The following example initializes the state and updates it using setNewNumber. All lifecycle methods are in the Content component.

React Example

class Button extends React.Component {
constructor(props) {
    super(props);
    this.state = {data: 0};
    this.setNewNumber = this.setNewNumber.bind(this);
}

setNewNumber() {
  this.setState({data: this.state.data + 1})
}
render() {
    return (
       <div>
          &lt;button onClick = {this.setNewNumber}>INCREMENT</button>
          <Content myNumber = {this.state.data}></Content>
       </div>
    );
  }
}


class Content extends React.Component {
componentWillMount() {
    console.log('Component WILL MOUNT!')
}
componentDidMount() {
     console.log('Component DID MOUNT!')
}
componentWillReceiveProps(newProps) {
      console.log('Component WILL RECEIVE PROPS!')
}
shouldComponentUpdate(newProps, newState) {
      return true;
}
componentWillUpdate(nextProps, nextState) {
      console.log('Component WILL UPDATE!');
}
componentDidUpdate(prevProps, prevState) {
      console.log('Component DID UPDATE!')
}
componentWillUnmount() {
       console.log('Component WILL UNMOUNT!')
}

  render() {
    return (
      <div>
        <h3>{this.props.myNumber}</h3>
      </div>
    );
  }
}
ReactDOM.render(
 <div>
    <Button />
 </div>,
document.getElementById('example')
);
❮ React Ref Render React Conditional Rendering ❯