in ReactJS by

What are the lifecycle methods of React?

1 Answer

0 votes
by

Before React 16.3

1) componentWillMount: Executed before rendering and is used for App level configuration in your root component.

2) componentDidMount: Executed after first rendering and here all AJAX requests, DOM or state updates, and set up event listeners should occur.

3) componentWillReceiveProps: Executed when particular prop updates to trigger state transitions.

4) shouldComponentUpdate: Determines if the component will be updated or not. By default it returns true. If you are sure that the component doesn't need to render after state or props are updated, you can return false value. It is a great place to improve performance as it allows you to prevent a re-render if component receives new prop.

5) componentWillUpdate: Executed before re-rendering the component when there are props & state changes confirmed by shouldComponentUpdate() which returns true.

6) componentDidUpdate: Mostly it is used to update the DOM in response to prop or state changes.

7) componentWillUnmount: It will be used to cancel any outgoing network requests, or remove all event listeners associated with the component.

React 16.3+

1) getDerivedStateFromProps: Invoked right before calling render() and is invoked on every render. This exists for rare use cases where you need derived state. Worth reading if you need derived state.

2) componentDidMount: Executed after first rendering and where all AJAX requests, DOM or state updates, and set up event listeners should occur.

3) shouldComponentUpdate: Determines if the component will be updated or not. By default it returns true. If you are sure that the component doesn't need to render after state or props are updated, you can return false value. It is a great place to improve performance as it allows you to prevent a re-render if component receives new prop.

4) getSnapshotBeforeUpdate: Executed right before rendered output is committed to the DOM. Any value returned by this will be passed into componentDidUpdate(). This is useful to capture information from the DOM i.e. scroll position.

5) componentDidUpdate: Mostly it is used to update the DOM in response to prop or state changes. This will not fire if shouldComponentUpdate() returns false.

6) componentWillUnmount It will be used to cancel any outgoing network requests, or remove all event listeners associated with the component.

...