当前位置: 首页 > web >正文

React-useImperativeHandle (forwardRef)

我们会遇到这样的场景:某个组件想要暴露一些方法,来供外部组件来调用。例如我们在开发form表单的时候,就需要把设置表单值、重置值、提交等方法暴露给外部使用。会有如下代码:

import { forwardRef } from 'react';const Form = forwardRef(function MyForm(props, ref) {useImperativeHandle(ref, () => {return {// ... 你的方法 ...};}, []);return (<div {...props} ref={ref}><input type="text" /></div>);
});

在组件外部,只需传入ref属性,即可调用form组件提供的方法。

获取最新的state

由于react中,setState之后,是采用异步调度、批量更新的策略,导致我们无法直接获取最新的state。在使用class组件的时候,我们可以通过传递第二个参数,传一个回调用函数,来让我们获取最新的state (在React 18以后,就算在class component里面,在setTimeout、原生事件回调里面,也是异步批量更新了)。在hooks里面,我目前只能通过useEffect,把当前state当作依赖传入,来在useEffect回调函数里面获取最新的state。
在setState的时候,其实就是在调用dispatchSetState,源码如下 (删掉了一些注释和DEV代码):

function dispatchSetState<S, A>(fiber: Fiber,queue: UpdateQueue<S, A>,action: A,
) // 计算更新优先级const lane = requestUpdateLane(fiber);const update: Update<S, A> = {lane,action,hasEagerState: false,eagerState: null,next: (null: any),};// 判断当前fiber是否正在处于更新中,若是则把当前更新进行排队if (isRenderPhaseUpdate(fiber)) {enqueueRenderPhaseUpdate(queue, update);} else {const alternate = fiber.alternate;if (fiber.lanes === NoLanes &&(alternate === null || alternate.lanes === NoLanes)) {const lastRenderedReducer = queue.lastRenderedReducer;if (lastRenderedReducer !== null) {let prevDispatcher;try {const currentState: S = (queue.lastRenderedState: any);const eagerState = lastRenderedReducer(currentState, action);update.hasEagerState = true;update.eagerState = eagerState;// 若新旧状态无变化,则直接返回,啥也不干if (is(eagerState, currentState)) {enqueueConcurrentHookUpdateAndEagerlyBailout(fiber, queue, update);return;}} catch (error) {// Suppress the error. It will throw again in the render phase.} finally {if (__DEV__) {ReactCurrentDispatcher.current = prevDispatcher;}}}}const root = enqueueConcurrentHookUpdate(fiber, queue, update, lane);if (root !== null) {const eventTime = requestEventTime();scheduleUpdateOnFiber(root, fiber, lane, eventTime);entangleTransitionUpdate(root, queue, lane);}}markUpdateInDevTools(fiber, lane, action);
}

scheduleUpdateOnFiber则是react内部的核心调度方法,源码如下:

export function scheduleUpdateOnFiber(root: FiberRoot,fiber: Fiber,lane: Lane,eventTime: number,
) {checkForNestedUpdates();// Mark that the root has a pending update.markRootUpdated(root, lane, eventTime);if ((executionContext & RenderContext) !== NoLanes &&root === workInProgressRoot) {warnAboutRenderPhaseUpdatesInDEV(fiber);// Track lanes that were updated during the render phaseworkInProgressRootRenderPhaseUpdatedLanes = mergeLanes(workInProgressRootRenderPhaseUpdatedLanes,lane,);} else {// This is a normal update, scheduled from outside the render phase. For// example, during an input event.if (enableUpdaterTracking) {if (isDevToolsPresent) {addFiberToLanesMap(root, fiber, lane);}}warnIfUpdatesNotWrappedWithActDEV(fiber);if (enableProfilerTimer && enableProfilerNestedUpdateScheduledHook) {if ((executionContext & CommitContext) !== NoContext &&root === rootCommittingMutationOrLayoutEffects) {if (fiber.mode & ProfileMode) {let current = fiber;while (current !== null) {if (current.tag === Profiler) {const {id, onNestedUpdateScheduled} = current.memoizedProps;if (typeof onNestedUpdateScheduled === 'function') {onNestedUpdateScheduled(id);}}current = current.return;}}}}if (enableTransitionTracing) {const transition = ReactCurrentBatchConfig.transition;if (transition !== null) {if (transition.startTime === -1) {transition.startTime = now();}addTransitionToLanesMap(root, transition, lane);}}if (root === workInProgressRoot) {if (deferRenderPhaseUpdateToNextBatch ||(executionContext & RenderContext) === NoContext) {workInProgressRootInterleavedUpdatedLanes = mergeLanes(workInProgressRootInterleavedUpdatedLanes,lane,);}if (workInProgressRootExitStatus === RootSuspendedWithDelay) {markRootSuspended(root, workInProgressRootRenderLanes);}}ensureRootIsScheduled(root, eventTime);if (lane === SyncLane &&executionContext === NoContext &&(fiber.mode & ConcurrentMode) === NoMode &&// Treat `act` as if it's inside `batchedUpdates`, even in legacy mode.!(__DEV__ && ReactCurrentActQueue.isBatchingLegacy)) {resetRenderTimer();flushSyncCallbacksOnlyInLegacyMode();}}
}

我们继续追踪ensureRootIsScheduled方法,此源码就省略了,然后会调用scheduleMicrotask方法,源码如下:


export const scheduleMicrotask: any =typeof queueMicrotask === 'function'? queueMicrotask: typeof localPromise !== 'undefined'? callback =>localPromise.resolve(null).then(callback).catch(handleErrorInNextTick): scheduleTimeout;

会优先使用queueMicrotask来添加一个微任务,此方法是一个标准的web api,可以不借助Promise来往微任务队列里面添加一个任务。若当前环境不支持queueMicrotask,则依次优先使用Promise,setTimeout。这与vue的nextTick源码实现是基本一致的。通过以上的分析,我们可以大致了解了react异步批量更新的调度过程。

http://www.xdnf.cn/news/364.html

相关文章:

  • 美信监控易:数据采集与整合的卓越之选
  • JSAPI2.2—日期
  • 蓝桥杯之递归
  • ClawCloud的免费空间(github用户登录可以获得$5元/月的免费额度)
  • java怎么完善注册,如果邮箱中途更换,能否判断
  • 【Flutter DevTools】性能优化的瑞士军刀
  • FFMPEG-视频解码-支持rtsp|rtmp|音视频文件(低延迟)
  • 基于MTF的1D-2D-CNN-GRU-Attention时序图像多模态融合的故障识别,适合研究学习(Matlab完整源码和数据),附模型研究报告
  • 基于springboot的个人财务管理系统的设计与实现
  • 【图片识别分类】如何快速识别照片中的水印文字,对图片进行关键字分类,快速整理水印相机拍摄图片,基于WPF和腾讯OCR的技术实现
  • 完美解决浏览器不能复制的问题(比如赛氪网的中题库练习题)
  • centos7更换yum源不生效
  • 原型模式详解及在自动驾驶场景代码示例(c++代码实现)
  • Java线程的几种状态
  • 文章记单词 | 第37篇(六级)
  • 最优树搜索策略
  • 民办生从零学C的第十一天:操作符
  • pip list | grep paho-mqtt 如何查看这个包的保存路径
  • 三轴云台之模块设计篇
  • 网工_FTP协议
  • QML 自定义组件外观和行为
  • 一个可以自定义Java服务名日志打印的小工具
  • TCP的三次握手和四次挥手
  • 【IDEA2020】 解决开发时遇到的一些问题
  • 中华传承-医山命相卜-铁板神数
  • 快速入门smolagents
  • FreeFileSync:文件同步对比工具
  • canal安装使用V1.1.4
  • 《数据牢笼》-来自DeepSeek
  • ‌2025年教育AI实战项目