`

instantiateReactComponent源码

 
阅读更多

instantiateReactComponent用于将ReactNode转化为用于挂载、更新到页面视图上的实例。

 

'use strict';

var _prodInvariant = require('./reactProdInvariant'),// 生产环境React形式带url报错
    _assign = require('object-assign');

// 用于更新或挂载用户自定义组件的协调器
var ReactCompositeComponent = require('./ReactCompositeComponent');

// 用于创建空组件
var ReactEmptyComponent = require('./ReactEmptyComponent');

// 用于创建React封装DOM标签组件、或文本组件
var ReactHostComponent = require('./ReactHostComponent');

var getNextDebugID = require('./getNextDebugID');

// invariant(condition,format,a,b,c,d,e,f) condition为否值,替换format中的"%s",并throw error报错
var invariant = require('fbjs/lib/invariant');

// warning(condition,format) condition为否值,替换format中的"%s",并console.error警告
var warning = require('fbjs/lib/warning');

// 继承ReactCompositeComponent,并将instantiateReactComponent赋值给原型方法,避免循环依赖
// 用于挂载用户自定义组件创建的元素
var ReactCompositeComponentWrapper = function (element) {
  this.construct(element);
};
_assign(ReactCompositeComponentWrapper.prototype, ReactCompositeComponent, {
  _instantiateReactComponent: instantiateReactComponent
});

// 适用于提示require加载的文件没有export
function getDeclarationErrorAddendum(owner) {
  if (owner) {
    var name = owner.getName();
    if (name) {
      return ' Check the render method of `' + name + '`.';
    }
  }
  return '';
}

// 内部组件书写形式,包含mountComponent、receiveComponent,用于挂载组件
// 用户自定义的组件包含render方法,指示待绘制的ReactNode包含哪些元素,挂载mountComponent却通过内部组件实现
function isInternalComponentType(type) {
  return typeof type === 'function' && typeof type.prototype !== 'undefined' && 
    typeof type.prototype.mountComponent === 'function' && 
    typeof type.prototype.receiveComponent === 'function';
}

// 参数node为ReactNode,包含ReactElement、ReactFragment、ReactText
//    其中,ReactElement又可分为ReactComponentElement、ReactDomElement两类
// 就ReactComponentElement为例,ReactComponentElement只设置了构造函数ReactComponent、props
//    其中,ReactComponent设定render方法用于创建ReactComponentElement元素下含的ReactNode
//    挂载到视图中,通过间接调用ReactCompositeComponent构造函数中的mountComponent方法实现
function instantiateReactComponent(node, shouldHaveDebugID) {
  var instance;

  // 空组件,由ReactEmptyComponent默认调用ReactDOMEmptyComponent创建
  if (node === null || node === false) {
    instance = ReactEmptyComponent.create(instantiateReactComponent);

  // React封装DOM标签组件、用户自定义组件处理
  } else if (typeof node === 'object') {
    var element = node;
    var type = element.type;

    // type类型错误报错,提示require加载的文件没有export
    if (typeof type !== 'function' && typeof type !== 'string') {
      var info = '';
      if (process.env.NODE_ENV !== 'production') {
        if (type === undefined || typeof type === 'object' && 
          type !== null && Object.keys(type).length === 0) {
          info += ' You likely forgot to export your component from the file ' 
            + 'it\'s defined in.';
        }
      }
      info += getDeclarationErrorAddendum(element._owner);
      !false ? process.env.NODE_ENV !== 'production' ? 
        invariant(false, 'Element type is invalid: expected a string (for built-in components) ' 
          + 'or a class/function (for composite components) but got: %s.%s', 
          type == null ? type : typeof type, info) 
        : _prodInvariant('130', type == null ? type : typeof type, info) : void 0;
    }

    // React封装DOM标签组件
    if (typeof element.type === 'string') {
      instance = ReactHostComponent.createInternalComponent(element);
    
    // React封装的内部组件(实现了mountComponent等方法)不能使用字符串引用的暂时替代方案
    } else if (isInternalComponentType(element.type)) {
      instance = new element.type(element);

      // 维持旧有api的有效性
      if (!instance.getHostNode) {
        instance.getHostNode = instance.getNativeNode;
      }

    // 用户自定义组件,创建原型继承ReactCompositeComponent类的ReactCompositeComponentWrapper实例
    } else {
      instance = new ReactCompositeComponentWrapper(element);
    }

  // 文本组件
  } else if (typeof node === 'string' || typeof node === 'number') {
    instance = ReactHostComponent.createInstanceForText(node);
  } else {
    !false ? process.env.NODE_ENV !== 'production' ? invariant(false, 'Encountered invalid React node of type %s', typeof node) : _prodInvariant('131', typeof node) : void 0;
  }

  if (process.env.NODE_ENV !== 'production') {
    process.env.NODE_ENV !== 'production' ? warning(typeof instance.mountComponent === 'function' && typeof instance.receiveComponent === 'function' && typeof instance.getHostNode === 'function' && typeof instance.unmountComponent === 'function', 'Only React Components can be mounted.') : void 0;
  }

  // 初始化参数
  instance._mountIndex = 0;
  instance._mountImage = null;

  if (process.env.NODE_ENV !== 'production') {
    instance._debugID = shouldHaveDebugID ? getNextDebugID() : 0;
  }

  // Object.preventExtensions使对象属性不可扩展,但可修改
  if (process.env.NODE_ENV !== 'production') {
    if (Object.preventExtensions) {
      Object.preventExtensions(instance);
    }
  }

  return instance;
}

module.exports = instantiateReactComponent;

 

1
0
分享到:
评论

相关推荐

    网狐6.6完整源码+内核源码+105款游戏源码(已解密).zip

    《网狐6.6完整源码与内核源码解析:105款游戏源码解密探索》 在IT行业中,源码是程序开发的核心,它揭示了软件的内部工作机制,是程序员进行二次开发、优化和调试的基础。"网狐6.6完整源码+内核源码+105款游戏源码...

    彩票网站源码_php彩票源码程序,彩票系统全套源码php

    【标题】"彩票网站源码_php彩票源码程序,彩票系统全套源码php"涉及到的知识点主要涵盖了彩票网站开发的核心技术、架构以及部署流程。彩票网站的开发通常基于Web技术,这里的源码采用PHP作为服务器端脚本语言,MySQL...

    PUBG雷达网页透视源码

    PUBG雷达网页透视源码PUBG雷达网页透视源码PUBG雷达网页透视源码PUBG雷达网页透视源码PUBG雷达网页透视源码PUBG雷达网页透视源码PUBG雷达网页透视源码PUBG雷达网页透视源码PUBG雷达网页透视源码PUBG雷达网页透视源码...

    SSCOM源码 DELPHI 源码

    SSCOM源码 DELPHI 源码 绝对源码!欢迎下载

    unity武侠游戏源码

    unity武侠角色扮演游戏源码unity武侠角色扮演游戏源码unity武侠角色扮演游戏源码unity武侠角色扮演游戏源码unity武侠角色扮演游戏源码unity武侠角色扮演游戏源码unity武侠角色扮演游戏源码unity武侠角色扮演游戏源码...

    autojs源码2000多源码,付费群源码

    自己搜集的源码,非常多。。一部分论坛 有了。一起打包吧。另一部分精品源码,都是来自己其他地方,主要是付费群和教学群。 2020/09/04 周五 23:52 400 1.数据类型.js 2020/09/04 周五 23:52 719 2.运算符.js 2020/...

    Unity精品农场经营游戏源码unity完整项目

    Unity游戏代码 ,Unity经典游戏源码 , Unity休闲游戏源码, Unity容易上手的游戏源码,Unity小游戏源码,Unity新手练习源码,Unity个人开发源码,Unity优秀项目,Unity商店优秀作品。 Unity精品农场经营游戏源码unity完整...

    微信小程序源码下载 微信小程序源码下载 2000套微信小程序源码

    本资源包含2000套微信小程序的源码,对于开发者来说是一份宝贵的参考资料,可以用来学习、研究或者作为开发新项目的起点。 源码下载是开发者获取程序原始代码的方式,对于学习和理解编程逻辑至关重要。这些微信小...

    网狐源码全套源码+详细架设教程

    【网狐源码全套源码+详细架设教程】是一个包含全面的网狐框架源代码和相关教程资源的集合,适合于对网狐框架感兴趣、想要深入学习或进行实际项目开发的IT从业者。这个压缩包提供了从基础到高级的全方位学习材料,...

    电商微信小程序源码+后台

    电商微信小程序源码+后台分享,亲测可用,有需要的朋友拿去!!! 电商微信小程序源码+后台分享,亲测可用,有需要的朋友拿去!!! 电商微信小程序源码+后台分享,亲测可用,有需要的朋友拿去!!! 电商微信小程序...

    微信小程序源码、小游戏源码总的70个!

    小程序源码、小游戏源码下载地址: https://gitee.com/giteesource/app/blob/master/resource/1008.md 包含源码有: 1、游戏小程序:轻松愉快的休闲时光 2、商城小程序:便捷的在线购物体验 3、工具小程序:实用的...

    借贷公司源码 网贷平台源码 php借贷源码

    【借贷公司源码 网贷平台源码 php借贷源码】这个标题揭示了我们要讨论的核心内容,即一套用于建立在线借贷或网贷平台的源代码,该源代码是基于PHP编程语言实现的。PHP是一种广泛使用的开源服务器端脚本语言,尤其在...

    变速齿轮 易语言源码 变速齿轮源码 变速器源码

    易语言源码就是用这种语言编写的程序代码,通过阅读和理解这些源码,开发者可以学习到如何利用易语言来实现特定功能,比如变速齿轮。 在易语言中实现变速齿轮功能,主要涉及到以下几个关键知识点: 1. **系统时间...

    饿了么源码 百度外卖源码 美团外卖源码 外卖系统源码

    订餐网,外卖网源码,带积分商城,商家系统,外卖网站建设! 系统特点: 周密策划、项目为先 "项目指导技术,技术服从项目",这是我们一贯秉承的原则,也是我们与其他系统开发商、网站建设公司的本质区别所在!我们...

    移动医疗APP源码 android (安卓版)妙手医生源码

    移动医疗APP源码是开发医疗健康应用的核心组成部分,它包含了应用程序的所有逻辑和界面设计。在Android平台上,这种源码通常是用Java或Kotlin语言编写的,并使用Android Studio作为集成开发环境(IDE)。在这个案例...

    cocos creator完整麻将源码下载

    《cocos creator完整麻将源码解析与开发指南》 cocos Creator是一款强大的2D游戏开发引擎,被广泛应用于游戏开发,尤其是休闲娱乐类游戏,如麻将。本篇将深入探讨"麻将源码"这一主题,结合cocos Creator的特性,为...

    微信H5游戏源码 H5游戏玫瑰夺宝源码 H5游戏竞猜游戏源码+视频搭建教程

    【H5游戏源码】微信H5游戏源码 H5游戏玫瑰夺宝源码 H5游戏竞猜游戏源码+视频搭建教程 H5游戏玫瑰夺宝源码源码已经测试,完全无问题可正常搭建,但是有无后门没有去检测,暂且时不知道后门的问题,免费提供给大家,...

    JAVA WEB 校园订餐系统项目源码+数据库

    JAVA WEB 校园订餐系统项目源码+数据库 JAVA WEB 校园订餐系统项目源码+数据库 JAVA WEB 校园订餐系统项目源码+数据库 JAVA WEB 校园订餐系统项目源码+数据库 JAVA WEB 校园订餐系统项目源码+数据库 JAVA WEB 校园...

    Java springboot医院管理系统源码 his源码+安装视频

    Java springboot医院管理系统源码 his源码+安装视频+文档 开发技术:springboot+shiro+layui+jquery+thymeleaf 运行环境:jdk8+mysql+IntelliJ IDEA+maven 1.门诊管理、2.住院管理、3.系统管理、4.统计管理、 5....

    ssh框架项目源码ssh框架项目源码ssh框架项目源码

    ssh框架项目源码ssh框架项目源码ssh框架项目源码ssh框架项目源码ssh框架项目源码ssh框架项目源码ssh框架项目源码ssh框架项目源码ssh框架项目源码ssh框架项目源码ssh框架项目源码ssh框架项目源码ssh框架项目源码ssh...

Global site tag (gtag.js) - Google Analytics