Each component in React has a life cycle that can be optimized to increase the effectiveness and performance of an application. As a beginner, you must understand every method in the lifecycle phase.
Like humans, react components have a lifecycle that can utilize to the maximum extent possible in making applications. When executed, the React component has a series of phases, and each phase provides methods that can be called when the component is in each phase. These functions are called reacts lifecycle methods.
Table of Contents
React Components Lifecycle For Dummy
By understanding the React component lifecycle, we can take certain actions, for example, when a component has created or when a component is about to be removed. In fact, with lifecycle methods, we can determine whether the component should be updated or not.
Look at the following diagram which to make it easier to understand the React Components Lifecycle.
The Reacts Lifecycle component has 3 main phases:
- Mounting phase
- Updating phases
- Unmounting phase
In each phase, The components execute the different methods known as reacts lifecycle methods.
1. Mounting Phase
This phase ran when the component rendered first to the DOM ie. Three main methods run in this phase, namely constructor, render, and componentDidMount.
When a component formed, the first method called is the constructor.
In addition to initializing the state values, a constructor method used to pass props to the extends reactcomponent.
The component class must always call the primary constructor with props.
The render method uses to execute the JSX scripts to the DOM. In this rendering method, we wrote HTML code in a JSX and executed it to the DOM.
After the rendering method executed, the componentDidMount method is called. This method used to manipulate the DOM usually used to request data from the API.
2. Updating phases
This updating phases is the phase when a component re-rendered. This phase occurs when a change in the state or props results in changes to the DOM.
In this phase, there are several methods to be executed.
Method is call determine whether a component to be rendered again or not. This method returns true or false values. If true, then the component re-rendered again or vice versa.
After the shouldComponentUpdate method returns true, then the render method is called again.
Same as componentDidMount, which is for manipulation of DOM and request data after the component is re-rendered.
Unmounting is the phase when a component removed from the DOM.
In this phase, only one method executed; namely, componentWillUnmount, which ran before a component removed from the DOM
There are several methods that I didn’t write down, and I explain in this article like getsnapshotbeforeupdate and getDerivedStateFromProps. Because basically, this method is very rare to use in several applications. But only for special cases
To better understand the components Lifecycle in ReactJS, Trying the following playground, see the console section.
- Components Lifecycle is easy for us to add the business rules before components rendered to the DOM.
- Lifecycle works every time there are changes to the state/props that result in changes to the DOM.
- Lifecycle make performance optimizations increases in React App.
- Props to stateful components declared in the constructor method
Thus the tutorial about the Lifecycle in ReactJS gets another react tutorial series on the React Tutorial For Beginners Series.