React有哪些生命周期钩子函数?数据请求放在哪个钩子里?

196 阅读1分钟

image.png

注意:函数式组件没有生命周期

  1. 挂载时调用 constructor,更新时不调用
  2. 更新时调用 shouldComponentUpdate 和 getSnapshotBeforeUpdate,挂载时不调用
  3. should... 在 render 前调用,getSnapshot... 在 render 后调用
  4. 请求放在 componentDidMount 里。

constructor不能放ajax请求是因为它会在ssr的时候被调用,服务器端拿不到数据 放在中间(render)可能会触发无限调用