React源码分析(一) React实例化过程

本文分析了React的实例化过程,包括ReactBaseClasses中的Component和PureComponent的区别,着重讲解了creactElement方法的详细步骤,如初始化参数、处理配置对象、子节点、defaultProps和保留属性。最后,阐述了ReactElement方法如何生成React节点,为后续的render过程做准备。
摘要由CSDN通过智能技术生成

实例化过程

一、ReactBaseClasses基类解析

1. Component

// 构建方法
function Component(props, context, updater) {
   
    this.props = props;
    this.context = context;
    // If a component has string refs, we will assign a different object   later.
    this.refs = emptyObject;
    // 我们初始化默认的更新程序,但是真正的更新程序由渲染器注入。
    this.updater = updater || ReactNoopUpdateQueue;
}
// 原型setState方法
Component.prototype.setState = function(partialState, callback) {
   
    //... 省略警告部分
    this.updater.enqueueSetState(this, partialState, callback, 'setState');
};
// 原型forceUpdate方法
Component.prototype.forceUpdate = function(callback) {
   
    this.updater.enqueueForceUpdate(this, callback, 'forceUpdate');
};

2. PureComponent

// 构建方法Component
function PureComponent(props, context, updater) {
   
    this.props = props;
    this.context = context;
    // 如果组件具有字符串引用,我们稍后将分配一个不同的
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值