搞定面试中的React生命周期函数

286 阅读1分钟

1. React v16.0的生命周期函数有哪些?

  • componentWillMount
  • render
  • componentDidMount
  • componentWillReciveProps
  • shouldComponentUpdate
  • componentWillUpdate
  • render
  • componentDidUpdate
  • componentWillUnmount

2. React v16.8前函数组件与类组件的区别是什么?

  • 函数组件
    • 语法上:纯函数
    • 状态管理:无状态组件、不可使用statesetState生命周期函数
  • 类组件
    • 语法上:继承React.Component并且创建render函数
    • 状态管理:可使用statesetState生命周期函数

3. React合成事件与DOM原生事件的区别是什么?

  • React合成事件
    • 驼峰
    • 事件委托至document节点
  • DOM原生事件
    • 小写
    • 事件绑定在DOM节点

4. setState是异步还是同步的?

  • 进入了React的调度流程,则为异步

  • 未进入React的调度流程,则为同步,如setTimeoutsetInterval直接在DOM上绑定原生事件