|
853 | 853 |
|
854 | 854 | 33. ### What are the different phases of component lifecycle?
|
855 | 855 |
|
856 |
| - There are four different phases of component lifecycle. |
| 856 | + The component lifecycle has three distinct lifecycle phases: |
857 | 857 |
|
858 |
| - 1. **Initialization:** In this phase component prepares setting up the initial state and default props. |
| 858 | + 1. **Mounting:** The component is ready to mount in the browser DOM. This phase covers initialization from `constructor()`, `getDerivedStateFromProps()`, `render()`, and `componentDidMount()` lifecycle methods. |
859 | 859 |
|
860 |
| - 2. **Mounting:** The component is ready to mount in the browser DOM. This phase covers `componentWillMount()` and `componentDidMount()` lifecycle methods. |
| 860 | + 2. **Updating:** In this phase, the component get updated in two ways, sending the new props and updating the state either from `setState()` or `forceUpdate()`. This phase covers `getDerivedStateFromProps()`, `shouldComponentUpdate()`, `render()`, `getSnapshotBeforeUpdate()` and `componentDidUpdate()` lifecycle methods. |
861 | 861 |
|
862 |
| - 3. **Updating:** In this phase, the component get updated in two ways, sending the new props and updating the state. This phase covers `shouldComponentUpdate()`, `componentWillUpdate()` and `componentDidUpdate()` lifecycle methods. |
| 862 | + 3. **Unmounting:** In this last phase, the component is not needed and get unmounted from the browser DOM. This phase includes `componentWillUnmount()` lifecycle method. |
863 | 863 |
|
864 |
| - 4. **Unmounting:** In this last phase, the component is not needed and get unmounted from the browser DOM. This phase includes `componentWillUnmount()` lifecycle method. |
865 |
| -  |
| 864 | + It's worth mentioning that React internally has a concept of phases when applying changes to the DOM. They are separated as follows |
| 865 | + |
| 866 | + 1. **Render** The component will render without any side-effects. This applies for Pure components and in this phase, React can pause, abort, or restart the render. |
| 867 | + |
| 868 | + 2. **Pre-commit** Before the component actually applies the changes to the DOM, there is a moment that allows React to read from the DOM through the `getSnapshotBeforeUpdate()`. |
| 869 | + |
| 870 | + 3. **Commit** React works with the DOM and executes the final lifecycles respectively `componentDidMount()` for mounting, `componentDidUpdate()` for updating, and `componentWillUnmount()` for unmounting. |
| 871 | + |
| 872 | + React 16.3+ Phases (or an [interactive version](http://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/)) |
| 873 | + |
| 874 | +  |
| 875 | + |
| 876 | + Before React 16.3 |
| 877 | + |
| 878 | +  |
866 | 879 |
|
867 |
| - <!-- TODO: new lifecycle methods in React v16 --> |
868 | 880 |
|
869 | 881 | 34. ### What are the lifecycle methods of React?
|
870 | 882 |
|
| 883 | + React 16.3+ |
| 884 | + |
| 885 | + - **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](https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html). |
| 886 | + - **componentDidMount:** Executed after first rendering and here all AJAX requests, DOM or state updates, and set up event listeners should occur. |
| 887 | + - **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. |
| 888 | + - **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. |
| 889 | + - **componentDidUpdate:** Mostly it is used to update the DOM in response to prop or state changes. This will not fire if `shouldComponentUpdate()` returns `false`. |
| 890 | + - **componentWillUnmount** It will be used to cancel any outgoing network requests, or remove all event listeners associated with the component. |
| 891 | + |
| 892 | + Before 16.3 |
| 893 | + |
871 | 894 | - **componentWillMount:** Executed before rendering and is used for App level configuration in your root component.
|
872 | 895 | - **componentDidMount:** Executed after first rendering and here all AJAX requests, DOM or state updates, and set up event listeners should occur.
|
873 | 896 | - **componentWillReceiveProps:** Executed when particular prop updates to trigger state transitions.
|
874 |
| - - **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. |
| 897 | + - **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. |
875 | 898 | - **componentWillUpdate:** Executed before re-rendering the component when there are props & state changes confirmed by `shouldComponentUpdate()` which returns true.
|
876 | 899 | - **componentDidUpdate:** Mostly it is used to update the DOM in response to prop or state changes.
|
877 | 900 | - **componentWillUnmount:** It will be used to cancel any outgoing network requests, or remove all event listeners associated with the component.
|
878 | 901 |
|
879 |
| - <!-- TODO: new lifecycle methods in React v16 --> |
880 |
| -
|
881 | 902 | 35. ### What are Higher-Order Components?
|
882 | 903 |
|
883 | 904 | A *higher-order component* (*HOC*) is a function that takes a component and returns a new component. Basically, it's a pattern that is derived from React's compositional nature.
|
|
0 commit comments