一张ES6中的React生命周期与流程图
生命周期分为三个阶段进行管理—挂载(MOUNTING)、渲染(RECEIVE_PROPS)、卸载(UNMOUNTING)
对应的方法分别是:mountComponent、componentUpdate、unmountComponent,每个方法都提供了几种处理方法,其中带will前缀的方法在进入状态之前调用,带did的在进入状态之后调用
阶段一:MOUNTING:
- constructor
- componentWillMount
- render
- componentDidMount
阶段二:RECEIVE_PROPS
- componentWillReceiveProps
- shouldComponentUpdate
- componentWillUpdate
- render
- componentDidUpdate
阶段三:UNMOUNTING
- componentWillUnmount
常见问题
在生命周期哪个方法里调用setState机制能触发render函数重新渲染?
componentDidMount()、render()和componentDidUpdate()
shouldComponentUpdate作用是啥以及为何他这么重要?
shouldComponentUpdate允许我们手动判断是否要进行组件更新,根据组件的应用场景设置函数的合理返回值能
够帮助我们性能优化,避免不必要的更新。禁止在shouldComponentUpdate和componentWillUpdate中调用setState,这样的话会造成循环调用,直至耗尽浏览器内存后崩溃