首页 > 编程语言 >#yyds干货盘点#react的useState源码分析

#yyds干货盘点#react的useState源码分析

时间:2023-11-11 10:35:26浏览次数:44  
标签:yyds const update react hook 源码 last null queue

简单说下为什么React选择函数式组件,主要是class组件比较冗余、生命周期函数写法不友好,骚写法多,functional组件更符合React编程思想等等等。更具体的可以拜读dan大神的blog。其中Function components capture the rendered values这句十分精辟的道出函数式组件的优势。

但是在16.8之前react的函数式组件十分羸弱,基本只能作用于纯展示组件,主要因为缺少state和生命周期。本人曾经在hooks出来前负责过纯函数式的react项目,所有状态处理都必须在reducer中进行,所有副作用都在saga中执行,可以说是十分艰辛的经历了。在hooks出来后我在公司的一个小中台项目中使用,落地效果不错,代码量显著减少的同时提升了代码的可读性。因为通过custom hooks可以更好地剥离代码结构,不会像以前类组件那样在cDU等生命周期堆了一大堆逻辑,在命令式代码和声明式代码中有一个良性的边界。

useState在React中是怎么实现的

Hooks take some getting used to — and especially at the boundary of imperative and declarative code.

如果对hooks不太了解的可以先看看这篇文章:前情提要,十分简明的介绍了hooks的核心原理,但是我对useEffect,useRef等钩子的实现比较好奇,所以开始啃起了源码,下面我会结合源码介绍useState的原理。useState具体逻辑分成三部分:mountState,dispatch, updateState

hook的结构

首先的是hooks的结构,hooks是挂载在组件Fiber结点上memoizedState的

//hook的结构
export type Hook = {
  memoizedState: any, //上一次的state
  baseState: any,  //当前state
  baseUpdate: Update<any, any> | null,  // update func
  queue: UpdateQueue<any, any> | null,  //用于缓存多次action
  next: Hook | null, //链表
};

renderWithHooks

在reconciler中处理函数式组件的函数是renderWithHooks,其类型是:

renderWithHooks(
  current: Fiber | null, //当前的fiber结点
  workInProgress: Fiber, 
  Component: any, //jsx中用<>调用的函数
  props: any,
  refOrContext: any,
  nextRenderExpirationTime: ExpirationTime, //需要在什么时候结束
): any

在renderWithHooks,核心流程如下:

//从memoizedState中取出hooks
nextCurrentHook = current !== null ? current.memoizedState : null; 
//判断通过有没有hooks判断是mount还是update,两者的函数不同
ReactCurrentDispatcher.current =
      nextCurrentHook === null
        ? HooksDispatcherOnMount
        : HooksDispatcherOnUpdate;
//执行传入的type函数
let children = Component(props, refOrContext);
//执行完函数后的dispatcher变成只能调用context的
ReactCurrentDispatcher.current = ContextOnlyDispatcher;

return children;

useState构建时流程

mountState

在HooksDispatcherOnMount中,useState调用的是下面的mountState,作用是创建一个新的hook并使用默认值初始化并绑定其触发器,因为useState底层是useReducer,所以数组第二个值返回的是dispatch。

type BasicStateAction<S> = (S => S) | S;

function mountState<S>(
  initialState: (() => S) | S,
){
  const hook = mountWorkInProgressHook();
//如果入参是func则会调用,但是不提供参数,带参数的需要包一层
  if (typeof initialState === 'function') {
    initialState = initialState();
  }
//上一个state和基本(当前)state都初始化
  hook.memoizedState = hook.baseState = initialState;
  const queue = (hook.queue = {
    last: null,
    dispatch: null,
    eagerReducer: basicStateReducer, // useState使用基础reducer
    eagerState: (initialState: any),
  });
//返回触发器
  const dispatch: Dispatch<
    //useState底层是useReducer,所以type是BasicStateAction
 (queue.dispatch = (dispatchAction.bind(
    null,
    //绑定当前fiber结点和queue
    ((currentlyRenderingFiber: any): Fiber),
    queue,
  ): any));
  return [hook.memoizedState, dispatch];
}

mountWorkInProgressHook

这个函数是mountState时调用的构建hook的方法,在初始化完毕后会连接到当前hook.next(如果有的话)

function mountWorkInProgressHook(): Hook {
  const hook: Hook = {
    memoizedState: null,
    baseState: null,
    queue: null,
    baseUpdate: null,
    next: null,
  };
  if (workInProgressHook === null) {
    // 列表中的第一个hook
    firstWorkInProgressHook = workInProgressHook = hook;
  } else {
    // 添加到列表的末尾
    workInProgressHook = workInProgressHook.next = hook;
  }
  return workInProgressHook;
}

dispatch分发函数

在上面我们提到,useState底层是useReducer,所以返回的第二个参数是dispatch函数,其中的设计十分巧妙。

假设我们有以下代码:

const [data, setData] = React.useState(0)
setData('first')
setData('second')
setData('third')

#yyds干货盘点#react的useState源码分析_函数式

在正常情况下,是不会在dispatcher中触发reducer而是将action存入update中在updateState中再执行,但是如果在react没有重渲染需求的前提下是会提前计算state即eagerState。作为性能优化的一环。相关参考视频讲解:进入学习

function dispatchAction<S, A>(
  fiber: Fiber,
  queue: UpdateQueue<S, A>,
  action: A,
) {
  const alternate = fiber.alternate;
   {
    flushPassiveEffects();
    //获取当前时间并计算可用时间
    const currentTime = requestCurrentTime();
    const expirationTime = computeExpirationForFiber(currentTime, fiber);

    const update: Update<S, A> = {
      expirationTime,
      action,
      eagerReducer: null,
      eagerState: null,
      next: null,
    };
    //下面的代码就是为了构建queue.last是最新的更新,然后last.next开始是每一次的action
    // 取出last
    const last = queue.last;
    if (last === null) {
      // 自圆
      update.next = update;
    } else {
      const first = last.next;
      if (first !== null) {

        update.next = first;
      }
      last.next = update;
    }
    queue.last = update;

    if (
      fiber.expirationTime === NoWork &&
      (alternate === null || alternate.expirationTime === NoWork)
    ) {
      // 当前队列为空,我们可以在进入render阶段前提前计算出下一个状态。如果新的状态和当前状态相同,则可以退出重渲染
      const lastRenderedReducer = queue.lastRenderedReducer; // 上次更新完后的reducer
      if (lastRenderedReducer !== null) {
        let prevDispatcher;
        if (__DEV__) {
          prevDispatcher = ReactCurrentDispatcher.current;  // 暂存dispatcher
          ReactCurrentDispatcher.current = InvalidNestedHooksDispatcherOnUpdateInDEV;
        }
        try {
          const currentState: S = (queue.lastRenderedState: any);
          // 计算下次state
          const eagerState = lastRenderedReducer(currentState, action);
          // 在update对象中存储预计算的完整状态和reducer,如果在进入render阶段前reducer没有变化那么可以服用eagerState而不用重新再次调用reducer
          update.eagerReducer = lastRenderedReducer;
          update.eagerState = eagerState;
          if (is(eagerState, currentState)) {
            // 在后续的时间中,如果这个组件因别的原因被重渲染且在那时reducer更变后,仍有可能重建这次更新
            return;
          }
        } catch (error) {
          // Suppress the error. It will throw again in the render phase.
        } finally {
          if (__DEV__) {
            ReactCurrentDispatcher.current = prevDispatcher;
          }
        }
      }
    }
    scheduleWork(fiber, expirationTime);
  }
}

useState更新时流程

updateReducer

因为useState底层是useReducer,所以在更新时的流程(即重渲染组件后)是调用updateReducer的。

function updateState<S>(
  initialState: (() => S) | S,
): [S, Dispatch<BasicStateAction<S>>] {
  return updateReducer(basicStateReducer, (initialState: any));
}

所以其reducer十分简单

function basicStateReducer<S>(state: S, action: BasicStateAction<S>): S {
  return typeof action === 'function' ? action(state) : action;
}

我们先把复杂情况抛开,跑通updateReducer流程

function updateReducer(
  reducer: (S, A) => S,
  initialArg: I,
  init?: I => S,
){
  // 获取当前hook,queue
  const hook = updateWorkInProgressHook();
  const queue = hook.queue;

  queue.lastRenderedReducer = reducer;

  // action队列的最后一个更新
  const last = queue.last;
  // 最后一个更新是基本状态
  const baseUpdate = hook.baseUpdate;
  const baseState = hook.baseState;

  // 找到第一个没处理的更新
  let first;
  if (baseUpdate !== null) {
    if (last !== null) {
      // 第一次更新时,队列是一个自圆queue.last.next = queue.first。当第一次update提交后,baseUpdate不再为空即可跳出队列
      last.next = null;
    }
    first = baseUpdate.next;
  } else {
    first = last !== null ? last.next : null;
  }
  if (first !== null) {
    let newState = baseState;
    let newBaseState = null;
    let newBaseUpdate = null;
    let prevUpdate = baseUpdate;
    let update = first;
    let didSkip = false;
    do {
      const updateExpirationTime = update.expirationTime;
      if (updateExpirationTime < renderExpirationTime) {
        // 优先级不足,跳过这次更新,如果这是第一次跳过更新,上一个update/state是newBaseupdate/state
        if (!didSkip) {
          didSkip = true;
          newBaseUpdate = prevUpdate;
          newBaseState = newState;
        }
        // 更新优先级
        if (updateExpirationTime > remainingExpirationTime) {
          remainingExpirationTime = updateExpirationTime;
        }
      } else {
        // 处理更新
        if (update.eagerReducer === reducer) {
          // 如果更新被提前处理了且reducer跟当前reducer匹配,可以复用eagerState
          newState = ((update.eagerState: any): S);
        } else {
          // 循环调用reducer
          const action = update.action;
          newState = reducer(newState, action);
        }
      }
      prevUpdate = update;
      update = update.next;
    } while (update !== null && update !== first);

    if (!didSkip) {
      newBaseUpdate = prevUpdate;
      newBaseState = newState;
    }

    // 只有在前后state变了才会标记
    if (!is(newState, hook.memoizedState)) {
      markWorkInProgressReceivedUpdate();
    }
    hook.memoizedState = newState;
    hook.baseUpdate = newBaseUpdate;
    hook.baseState = newBaseState;
    queue.lastRenderedState = newState;
  }

  const dispatch: Dispatch<A> = (queue.dispatch: any);
  return [hook.memoizedState, dispatch];
}
export function markWorkInProgressReceivedUpdate() {
  didReceiveUpdate = true;
}

标签:yyds,const,update,react,hook,源码,last,null,queue
From: https://blog.51cto.com/u_11365839/8315011

相关文章

  • 医学影像系统源码(MRI、CT三维重建)
    一、MRI概述核磁共振成像(英语:NuclearMagneticResonanceImaging,简称NMRI),又称自旋成像(英语:spinimaging),也称磁共振成像(MagneticResonanceImaging,简称MRI),台湾又称磁振造影,香港又称磁力共振扫描,是利用核磁共振(nuclearmagneticresonance,简称NMR)原理,依据所释放的能量在物质内部不......
  • Qt源码解析——元对象系统热身
    关键词:Qt源码QObjectQMetaObject元对象系统属性事件信号槽概述原系列文章地址官方文档第二章内容就是元对象系统,它在介绍里描述到:Qt的元对象系统提供了信号和槽机制(用于对象间的通信)、运行时类型信息和动态属性系统。元对象系统基于三个要素:QObject类为那些可以利......
  • Linux软件包(源码包和二进制包)
    Linux下的软件包众多,且几乎都是经GPL授权、免费开源(无偿公开源代码)的。这意味着如果你具备修改软件源代码的能力,只要你愿意,可以随意修改。GPL,全称GeneralPublicLicense,中文名称“通用性公开许可证”,简单理解GPL就是一个保护软件自由的一个协议,经GPL协议授权的软件必须开源......
  • Vue源码学习(十六):diff算法(三)暴力比对
    好家伙,这是diff的最后一节了 0.暴力比对的使用场景 没有可复用的节点:当新旧虚拟DOM的结构完全不同,或者某个节点不能被复用时,需要通过暴力比对来创建新的节点,并在真实DOM上进行相应的插入操作。0.1.例子一://创建vnodeletvm1=newVue({data:{name:'张三'......
  • APISIX源码安装问题解决
    官网手册的安装语句:curlhttps://raw.githubusercontent.com/apache/apisix/master/utils/install-dependencies.sh-sL|bash-执行install-dependencies.sh报如下错误:Transactioncheckerror:file/usr/share/gcc-4.8.2/python/libstdcxx/v6/printers.pyfrominstal......
  • 智慧工地源码:建筑管理的新型方式
    智慧工地是利用物联网、云计算、大数据等技术,实现对建筑工地实时监测、管理和控制的一种新型建筑管理方式,其中,数据分析功能起着至关重要的作用。1、数据采集智慧工地中的数据采集主要通过传感器、监控摄像头等设备进行。这些设备可以实时采集到工地的环境数据、施工人员和设备信息......
  • (四)Spring源码解析:bean的加载流程解析
    一、概述在前几讲中,我们着重的分析了Spring对xml配置文件的解析和注册过程。那么,本节内容,将会试图分析一下bean的加载过程。具体代码,如下图所示:1.1>doGetBean(...)针对bean的创建和加载,我们可以看出来逻辑都是在doGetBean(...)这个方法中的,所以,如下就是针对于这个方法的整体源码注......
  • react组件间通信
    1.父组件向子组件通信import{useState}from'react';functionButton(props){return(<div>{props.name}</div>)}functionApp(){constmsg=useState('神雕侠侣')return(<divclassName="App">&l......
  • TiDB 源码编译之 TiProxy 篇
    作者:ShawnYanTiProxy简介TiProxy是一个基于Apache2.0协议开源的、轻量级的TiDB数据库代理,基于Go语言编写,支持MySQL协议。TiProxy支持负载均衡,接收来自应用程序的请求,然后将其发送到TiDB集群。支持自动故障转移,当后端TiDBServer发生故障,可以自动将连接转移到......
  • 编译spring5.2.x源码
    编译spring源码是学习spring的第一步,spring源码是用gradle编译的,在编译时会遇到一些问题,在此记录一下。下载spring源码第一步需要下载spring的源码,由于官方托管在github,国内访问github不通畅,这里就直接使用gitee的镜像仓库,在国内比较快,地址:https://gitee.com/mirrors/spring-fra......