当前位置:首页 > React

react源码如何处理生命周期

2026-01-26 05:21:36React

React 生命周期处理机制

React 的生命周期处理主要分为三个阶段:挂载(Mounting)、更新(Updating)和卸载(Unmounting)。React 内部通过协调算法(Reconciliation)和 Fiber 架构管理这些生命周期。

挂载阶段(Mounting)

组件实例被创建并插入 DOM 时触发:

  • constructor:初始化状态和绑定方法。
  • static getDerivedStateFromProps:在渲染前根据新的 props 计算状态。
  • render:生成虚拟 DOM。
  • componentDidMount:DOM 挂载完成后执行,适合发起网络请求或订阅事件。
class Example extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
  }

  static getDerivedStateFromProps(nextProps, prevState) {
    return null; // 返回更新的状态或 null
  }

  componentDidMount() {
    console.log('Component mounted');
  }

  render() {
    return <div>{this.state.count}</div>;
  }
}

更新阶段(Updating)

props 或 state 变化时触发:

react源码如何处理生命周期

  • static getDerivedStateFromProps:与挂载阶段相同。
  • shouldComponentUpdate:返回布尔值决定是否重新渲染,默认返回 true
  • render:生成新的虚拟 DOM。
  • getSnapshotBeforeUpdate:DOM 更新前捕获信息(如滚动位置)。
  • componentDidUpdate:DOM 更新后执行,可操作 DOM 或发起请求。
shouldComponentUpdate(nextProps, nextState) {
  return nextState.count !== this.state.count;
}

getSnapshotBeforeUpdate(prevProps, prevState) {
  return { scrollPosition: window.scrollY };
}

componentDidUpdate(prevProps, prevState, snapshot) {
  if (snapshot.scrollPosition !== window.scrollY) {
    window.scrollTo(0, snapshot.scrollPosition);
  }
}

卸载阶段(Unmounting)

组件从 DOM 中移除时触发:

  • componentWillUnmount:清理定时器、取消订阅等。
componentWillUnmount() {
  clearInterval(this.timerID);
}

Fiber 架构下的生命周期

React 16 引入 Fiber 架构后,生命周期分为三个阶段:

react源码如何处理生命周期

  1. Render 阶段:可中断,包含 getDerivedStateFromPropsshouldComponentUpdate
  2. Pre-commit 阶段:不可中断,包含 getSnapshotBeforeUpdate
  3. Commit 阶段:同步执行,包含 componentDidMountcomponentDidUpdate

废弃的生命周期方法

React 17+ 废弃以下方法:

  • componentWillMount
  • componentWillReceiveProps
  • componentWillUpdate

替代方案:

  • 使用 static getDerivedStateFromProps 替代 componentWillReceiveProps
  • 使用 getSnapshotBeforeUpdate 替代 componentWillUpdate

函数组件与 Hooks

函数组件通过 Hooks 模拟生命周期:

  • useEffect:组合 componentDidMountcomponentDidUpdatecomponentWillUnmount
  • useMemo/useCallback:优化性能,类似 shouldComponentUpdate
function Example() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    console.log('Component mounted or updated');
    return () => console.log('Component will unmount');
  }, [count]); // 依赖数组控制触发条件
}

分享给朋友:

相关文章

java如何处理高并发

java如何处理高并发

Java处理高并发的方法 使用多线程和线程池 Java的java.util.concurrent包提供了线程池(如ThreadPoolExecutor)和并发工具类(如CountDownLatch、C…

vue生命周期实现

vue生命周期实现

Vue 生命周期实现 Vue 的生命周期钩子是框架在组件创建、更新、销毁过程中自动调用的函数,开发者可以通过这些钩子在不同阶段执行自定义逻辑。以下是 Vue 生命周期的主要阶段及其实现方式: 生命周…

react如何处理动画

react如何处理动画

处理动画的常用方法 React中处理动画可以通过多种方式实现,包括CSS动画、第三方库或React内置API。以下是几种主流方法: CSS过渡与关键帧动画 直接在组件的CSS中定义transiti…

react如何处理异步

react如何处理异步

异步处理的核心方法 在React中处理异步操作主要依赖以下几种方式: useEffect钩子 适用于组件挂载、更新或卸载时的副作用操作。结合async/await语法可清晰管理异步流程: useE…

react 如何处理时间戳

react 如何处理时间戳

时间戳转换为可读格式 使用 new Date() 将时间戳转换为日期对象,再通过内置方法格式化输出。例如显示为 YYYY-MM-DD HH:MM:SS: const timestamp = 1625…

react如何模拟生命周期

react如何模拟生命周期

React 模拟生命周期的方法 在 React 中,函数组件通过 Hooks 可以模拟类组件的生命周期行为。以下是常见生命周期的模拟方式: componentDidMount 使用 useEffe…