react源码解析12.状态更新流程

react源码解析12.状态更新流程

视频讲解(高效学习):进入学习
setState&forceUpdate

在react中触发状态更新的几种方式:

  • ReactDOM.render
  • this.setState
  • this.forceUpdate
  • useState
  • useReducer

我们重点看下重点看下this.setState和this.forceUpdate,hook在第13章讲

  1. this.setState内调用this.updater.enqueueSetState,主要是将update加入updateQueue中

    //ReactBaseClasses.js
    Component.prototype.setState = function (partialState, callback) {
         
      if (!(typeof partialState === 'object' || typeof partialState === 'function' || partialState == null)) {
         
        {
         
          throw Error( "setState(...): takes an object of state variables to update or a function which returns an object of state variables." );
        }
      }
      this.updater.enqueueSetState(this, partialState, callback, 'setState');
    };
    
    
    //ReactFiberClassComponent.old.js
    enqueueSetState(inst, payload, callback) {
         
      const fiber = getInstance(inst);//fiber实例
    
      const eventTime = requestEventTime();
      const suspenseConfig = requestCurrentSuspenseConfig();
      
      const lane = requestUpdateLane(fiber, suspenseConfig);//优先级
    
      const update = createUpdate(eventTime, lane, suspenseConfig);//创建update
    
      update.payload = payload;
    
      if (callback !== undefined && callback !== null) {
           //赋值回调
        update.callback = callback;
      }
    
      enqueueUpdate(fiber, update);//update加入updateQueue
      scheduleUpdateOnFiber(fiber, lane, eventTime);//调度update
    }
    

    enqueueUpdate用来将update加入updateQueue队列

    //ReactUpdateQueue.old.js
    export function enqueueUpdate<State>(fiber: Fiber, update: Update<State>) {
         
      const updateQueue = fiber.updateQueue;
      if (updateQueue === null) {
         
        return;
      }
    
      const sharedQueue: SharedQueue<State> = (updateQueue: any).shared;
      const pending = sharedQueue.pending;
      if (pending === null) {
         
        update.next = update;//与自己形成环状链表
      } else {
         
        update.next = pending.next;//加入链表的结尾
        pending.next = update;
      }
      sharedQueue.pending = update;
    }
    

    react源码12.6

  2. this.forceUpdate和this.setState一样,只是会让tag赋值ForceUpdate

    //ReactBaseClasses.js
    Component.prototype.forceUpdate = function
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Native中的VirtualizedList组件是用于高效渲染大量数据的列表视图组件。虚拟化列表在滚动时只加载可见部分的数据,而非一次性渲染所有的列表项,从而提高了性能。 VirtualizedList组件的源码解析可以从以下几个方面进行: 1. 数据源:VirtualizedList接受一个名为data的props,用于表示要渲染的数据源。该数据源可以是一个数组或者是一个带有迭代方法的对象。在源码中,使用this.props.data来获取传入的数据源。 2. 视图创建:在VirtualizedList组件的源码中,通过ViewabilityHelper类型来管理可见项和滚动状态。ViewabilityHelper根据滚动位置计算哪些列表项是可见的,并且在必要时创建和删除列表项的视图。 3. 渲染性能优化:为了提高滚动的性能,VirtualizedList使用了windowSize属性,该属性定义了可见区域外额外渲染的列表项数量。源码根据滚动的位置,动态加载和卸载视图,以保证只渲染用户可见的列表项。 4. 列表项更新:当列表项的数据发生变化时,VirtualizedList根据数据的变化更新相应的列表项。在源码中,使用shouldItemUpdate()方法来判断列表项是否需要更新。 5. 交互处理:VirtualizedList对滚动事件、滚动结束事件等进行处理,以便实现列表的滚动效果。在源码中,使用onScroll()和onScrollEndDrag()等方法来处理相关交互事件。 总的来说,VirtualizedList作为React Native中高效渲染大量数据的列表视图组件,其源码实现了数据源管理、视图创建和销毁、渲染性能优化、列表项更新、交互处理等功能。通过深入源码的分析,可以更好地了解VirtualizedList组件的工作原理,并能更好地使用和定制该组件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值