Lifecycle methods in React
A component in React has three phases of lifecycle:
1. Mounting Phase
Mounting means adding elements into the DOM.
React provides some build in methods which are called when elements are mounted into the DOM and there methods are:
- constructor(): in class Component, it is called before anything else. Hence it is used to set up the initial state of the component.
- componentDidMount(): It is called when a component is rendered
2. Updation Phase
This is next phase after mounting. It is used to keep track of changes in a component.
A class or functional component updates itself whenever any change occurs in state or props value. React provides the following methods to call, when a component is updated:
- shouldComponentUpdate(): returns a boolean value that specifies whether React shold continue rendering or not.
- componentDidUpdate(): called after the component is updated in the DOM.
3. Unmounting Phase
This is the last phase. When a component is removed from the DOM, called unmounting.
React provides the following method to handle unmounting of component:
- componentWillUnmount(): called when the component is about to be removed from the DOM.
Comments
Post a Comment