zoukankan      html  css  js  c++  java
  • react context 源码解读

    源码地址

    注: 这里,为了读起来思路更清晰一些,我们将__DEV__的代码去掉。

    import {REACT_PROVIDER_TYPE, REACT_CONTEXT_TYPE} from 'shared/ReactSymbols';
    
    import type {ReactContext} from 'shared/ReactTypes';
    
    export function createContext<T>(
      defaultValue: T,
      calculateChangedBits: ?(a: T, b: T) => number,
    ): ReactContext<T> {
      if (calculateChangedBits === undefined) {
        calculateChangedBits = null;
      }
    
      const context: ReactContext<T> = {
        $$typeof: REACT_CONTEXT_TYPE,
        _calculateChangedBits: calculateChangedBits,
        // As a workaround to support multiple concurrent renderers, we categorize
        // some renderers as primary and others as secondary. We only expect
        // there to be two concurrent renderers at most: React Native (primary) and
        // Fabric (secondary); React DOM (primary) and React ART (secondary).
        // Secondary renderers store their context values on separate fields.
        _currentValue: defaultValue,
        _currentValue2: defaultValue,
        // Used to track how many concurrent renderers this context currently
        // supports within in a single renderer. Such as parallel server rendering.
        _threadCount: 0,
        // These are circular
        Provider: (null: any),
        Consumer: (null: any),
      };
    
      context.Provider = {
        $$typeof: REACT_PROVIDER_TYPE,
        _context: context,
      };
    
       context.Consumer = context;
    
      return context;
    }

    全局创建的context实例,对外提供Provider和Consumer两个属性,Provider的_context属性值又指向context实例本身,即:当改变Provider组件value的值,会作用到Provider内部的_context属性,进而更新context的_currentValue. 而

    context.Consumer = context

    所以会触发consumer的更新。

     

    参考:https://xie.infoq.cn/article/d56577c78e76508722e37025f

  • 相关阅读:
    Ionic入门一:Hello Ionic
    华为p7怎么打开usb调试模式
    Mac系统如何配置adb
    在Mac中如何显示和隐藏文件
    MAC OSX环境下cordova+Ionic的安装配置
    在Eclipse中怎样写Java注释
    Springmvc数据校验
    SpringMVC核心类DispatcherServlet
    Spring MVC常用的注解类
    为SpringMvc项目安装BootStrap和AngularJs前端框架
  • 原文地址:https://www.cnblogs.com/ceceliahappycoding/p/14365804.html
Copyright © 2011-2022 走看看