官网链接:https://zh-hans.reactjs.org/docs/react-component.html

一、元素的四个阶段

想象一下元素渲染到页面、到更新、到最后移除,它经历了哪些阶段

  1. 元素创建:create
  2. 元素挂载:mount
  3. 元素更新:update
  4. 元素消亡:unmount

二、React 组件的生命周期

可以记住 React 的八个常用的生命周期钩子

  1. constructor
  2. componentWillMount(过时)
  3. render
  4. componentDidMount
  5. componentWillUpdate(过时)
  • render
  1. componentDidUpdate
  2. componentWillUnmount
  3. componentWillReceiveProps(过时)

还有一个 shouldComponentUpdate,这个钩子可以通过 return false 阻止组件更新,它的作用如下

  • 允许我们手动地判断是否要进行组件更新
  • 设置函数的合理返回值避免不必要的更新

image.png

三、不同的操作常常放在哪里?

  1. 请求数据
    1. 各个事件中
    2. componentDidMount
  2. 初始化数据
    1. constructor
  3. 更新数据
    1. 各个事件中
    2. componentDidMount