React_Updated
React_Updated
Constructor:
When you implement the constructor for a React component, you need to
call super(props) method before any other statement. If you do not call
super(props) method, this.props will be undefined in the constructor and can lead
to bugs.
Syntax
Constructor(props){
super(props);
}
Note: It used for binding event handler methods that occur in your component.
Each component has several “lifecycle methods” that you can override to run
code at particular times in the process.
https://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/
A component's lifecycle has three main phases: the Initial Phase,Mounting Phase,
the Updating Phase, and the Unmounting Phase.
1.Initial Phase
It is the birth phase of the lifecycle of a ReactJS component. Here, the component
starts its journey on a way to the DOM. In this phase, a component contains the
default Props and initial State. These default properties are done in the
constructor of a component. The initial phase only occurs once and consists of the
following methods.
o getDefaultProps()
It is used to specify the default value of this.props. It is invoked before the
creation of the component or any props from the parent is passed into it.
o getInitialState()
It is used to specify the default value of this.state. It is invoked before the
creation of the component.
2. Mounting Phase
In this phase, the instance of a component is created and inserted into the DOM.
It consists of the following methods.
o componentWillMount()
This is invoked immediately before a component gets rendered into the
DOM. In the case, when you call setState() inside this method, the
component will not re-render.
o componentDidMount()
This is invoked immediately after a component gets rendered and placed
on the DOM. Now, you can do any DOM querying operations.
o render()
This method is defined in each and every component. It is responsible for
returning a single root HTML node element. If you don't want to render
anything, you can return a null or false value.
3.Updating Phase
o componentWillRecieveProps()
It is invoked when a component receives new props. If you want to update
the state in response to prop changes, you should compare this.props and
nextProps to perform state transition by using this.setState() method.
o shouldComponentUpdate()
It is invoked when a component decides any changes/updation to the DOM.
It allows you to control the component's behavior of updating itself. If this
method returns true, the component will update. Otherwise, the
component will skip the updating.
o componentWillUpdate()
It is invoked just before the component updating occurs. Here, you can't
change the component state by invoking this.setState() method. It will not
be called, if shouldComponentUpdate() returns false.
o render()
It is invoked to examine this.props and this.state and return one of the
following types: React elements, Arrays and fragments, Booleans or null,
String and Number. If shouldComponentUpdate() returns false, the code
inside render() will be invoked again to ensure that the component displays
itself properly.
o componentDidUpdate()
It is invoked immediately after the component updating occurs. In this
method, you can put any code inside this which you want to execute once
the updating occurs. This method is not invoked for the initial render.
4. Unmounting Phase
o componentWillUnmount()
This method is invoked immediately before a component is destroyed and
unmounted permanently. It performs any necessary cleanup related task
such as invalidating timers, event listener, canceling network requests, or
cleaning up DOM elements. If a component instance is unmounted, you
cannot mount it again.