Overview

In react there are three phases in a component: MountingUpdating and UnmountingReact life cycle diagram

Mounting

The mounting phase is when the component is being created and is inserted into the DOM. The methods that are called in order for this phase are:

constructor()

The constructor() method is called before it is mounted. It is not necessary to define the constructor method. Typically the constructor method is used for initializing state or binding methods or both. If the component is taking in props it should be an argument when defining the method. Props should not be copied into state. Also the constructor should not call setState().

constructor(props) {
   super(props); // Necessary to access props in constructor
  this.state = { counter: 0 };
  this.handleClick = this.handleClick.bind(this);
}

#javascript #react-lifecycle #react #reactjs #front-end-development

React Lifecycle
1.20 GEEK