|
875 | 875 |
|
876 | 876 | 3. **Unmounting:** 在这个最后阶段,不需要组件,它将从浏览器 DOM 中卸载。这个阶段包含 `componentWillUnmount()` 生命周期方法。
|
877 | 877 |
|
878 |
| - It's worth mentioning that React internally has a concept of phases when applying changes to the DOM. They are separated as follows |
879 | 878 | 值得一提的是,在将更改应用到 DOM 时,React 内部也有阶段概念。它们按如下方式分隔开:
|
880 | 879 |
|
881 |
| - 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. |
| 880 | + 1. **Render** 组件将会进行无副作用渲染。这适用于纯组件(Pure Component),在此阶段,React 可以暂停,中止或重新渲染。 |
882 | 881 |
|
883 |
| - 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()`. |
| 882 | + 2. **Pre-commit** 在组件实际将更改应用于 DOM 之前,有一个时刻允许 React 通过`getSnapshotBeforeUpdate()`捕获一些 DOM 信息(例如滚动位置)。 |
884 | 883 |
|
885 |
| - 3. **Commit** React works with the DOM and executes the final lifecycles respectively `componentDidMount()` for mounting, `componentDidUpdate()` for updating, and `componentWillUnmount()` for unmounting. |
886 |
| - |
887 |
| - React 16.3+ Phases (or an [interactive version](http://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/)) |
| 884 | + 3. **Commit** React 操作 DOM 并分别执行最后的生命周期: `componentDidMount()` 在 DOM 渲染完成后调用, `componentDidUpdate()` 在组件更新时调用, `componentWillUnmount()` 在组件卸载时调用。 |
| 885 | + React 16.3+ 阶段 (也可以看[交互式版本](http://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/)) |
888 | 886 |
|
889 | 887 | 
|
890 | 888 |
|
891 |
| - Before React 16.3 |
| 889 | + React 16.3 之前 |
892 | 890 |
|
893 | 891 | 
|
894 | 892 |
|
895 | 893 |
|
896 |
| -34. ### What are the lifecycle methods of React? |
| 894 | +34. ### React 生命周期方法有哪些? |
897 | 895 |
|
898 | 896 | React 16.3+
|
899 | 897 |
|
900 |
| - - **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). |
901 |
| - - **componentDidMount:** Executed after first rendering and here all AJAX requests, DOM or state updates, and set up event listeners should occur. |
902 |
| - - **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. |
903 |
| - - **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. |
904 |
| - - **componentDidUpdate:** Mostly it is used to update the DOM in response to prop or state changes. This will not fire if `shouldComponentUpdate()` returns `false`. |
905 |
| - - **componentWillUnmount** It will be used to cancel any outgoing network requests, or remove all event listeners associated with the component. |
| 898 | + - **getDerivedStateFromProps:** 在调用`render()`之前调用,并在 *每次* 渲染时调用。 需要使用派生状态的情况是很罕见得。值得阅读 [如果你需要派生状态](https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html). |
| 899 | + - **componentDidMount:** 首次渲染后调用,所有得 Ajax 请求、DOM 或状态更新、设置事件监听器都应该在此处发生。 |
| 900 | + - **shouldComponentUpdate:** 确定组件是否应该更新。 默认情况下,它返回`true`。 如果您确定在更新状态或属性后不需要渲染组件,则可以返回`false`值。 它是一个提高性能的好地方,因为它允许您在组件接收新属性时阻止重新渲染。 |
| 901 | + - **getSnapshotBeforeUpdate:** 在最新的渲染输出提交给 DOM 前将会立即调用,这对于从 DOM 捕获信息(比如:滚动位置)很有用。 |
| 902 | + - **componentDidUpdate:** 它主要用于更新 DOM 以响应 prop 或 state 更改。 如果`shouldComponentUpdate()`返回`false`,则不会触发。 |
| 903 | + - **componentWillUnmount** 当一个组件被从 DOM 中移除时,该方法被调用,取消网络请求或者移除与该组件相关的事件监听程序等应该在这里进行。 |
906 | 904 |
|
907 | 905 | Before 16.3
|
908 | 906 |
|
909 |
| - - **componentWillMount:** Executed before rendering and is used for App level configuration in your root component. |
| 907 | + - **componentWillMount:** 在组件`render()`前执行,用于根组件中的应用程序级别配置。应该避免在该方法中引入任何的副作用或订阅。 |
910 | 908 | - **componentDidMount:** Executed after first rendering and here all AJAX requests, DOM or state updates, and set up event listeners should occur.
|
911 | 909 | - **componentWillReceiveProps:** Executed when particular prop updates to trigger state transitions.
|
912 | 910 | - **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.
|
|
0 commit comments