logo

该视频仅会员有权观看

立即开通课程「React.js 前端应用开发实践」权限。

¥
199
/ 年

React 组件的生命周期(Lifecycle)

  • Rex组件提供生命周期方法以在不同阶段执行任务。
  • 创建时执行construct,挂载后执行component demount,更新时执行component dead update,取消挂载前执行component will on mount
  • AppHeader组件中,使用component deep amount在挂载后执行定时任务,使用setInterval移至该方法中。
  • 挂载取消后,需要停止setInterval定时任务,通过component will on mount使用window.clearInterval来停止。
  • 使用timer变量存储setInterval返回值,用于取消时清除定时器。
  • 更新后使用component eat update输出状态更新信息。
  • 组件取消挂载后停止执行setInterval任务。