Explain the initial cycle in React which happens during the first render ?
We have seen different phases of Lifecycle methods initialization, mounting, updating, and unmounting in which the mounting phase plays a major role. So first let’s understand the mounting phase and its sub-methods which involve the lifecycle and then we come to know the initial cycle in the first render with the help of running code examples.
Mounting: Methods which are called when an instance of the component is being created and inserted into the dom.
Prerequisite: LifeCycle Method in ReactJs
The methods called in order during mounting in class-based component:
This is a special function that will be called whenever a new component is created
Binding state and event handlers
Do not cause side effects like making ajax calls
The super(props) called base class constructor and overwrite this.state
When state of the component depends upon changes in props over time
Initial state of component depends upon the props being passed to the component in such case this method is used to set the state
It does not have access to this keyword so it simply return an object that represent new state
The required() method in class component
Simply read this.props and this.state and return JSX
It is a pure function
Do not change state or interact with DOM or making ajax calls
Children component life cycle method also get executed
Whenever there is mounting and update cycle render function could get run and update cycle happens whenever props changes and state update.
Invoked only once in whole life cycle of methods
Invoked immediately after component and all its children components have been rendered to the dom cause side effects
Load data by making network requests
So this cycle or order described above is initially followed during the first render.
Creating React Application:
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
Example: This code will run in the project structure filename mentioned below. In this example we have to show that these sequence is followed using console.log
Steps to start the application: Write the below code in the terminal to start up the application:
Please Login to comment...