1.

Explain the initial cycle in React which happens during the first render?

Answer»

When a React App loads for the first time, the code is RUN in the mentioned order. All the below-mentioned methods run only one time except “render()”, which can be run many times depending on setState and Parent component bee called.

1) The “constructor()” is the first thing to be called. You can set the initial state here like below.

 constructor(){    super();    this.state = {      spinLogo: true    };  }


2) Then the “componentWillMount()” will be called. It is very similar to the constructor and called only once before the initial mounting of DOM. That is the reason, it doesn’t have access to the DOM.  ReactJS documentation recommends us to use constructor INSTEAD of this lifecycle method and it will be soon DEPRECATED

3) Then the initial “render()” will be called. It will also render all the child components(if any) of this component. Also, note that render is generally called many times. WHENEVER we use setState, the component render is called.

4) Then the function “componentDidMount()” will be called. This function will also be called once during the whole life-cycle. It is a great place to do AJAX call to the server to fetch some data. You can also initialize something that requires interaction with the DOM, like a jQuery library.



Discussion

No Comment Found