Component
// Component 类构造函数function Component(props, context, updater) { // 初始化 props, context this.props = props; this.context = context; // If a component has string refs, we will assign a different object later. // 初始化 refs 为空对象 this.refs = emptyObject; // We initialize the default updater but the real one gets injected by the // renderer. // 更新渲染用的函数 updater this.updater = updater || ReactNoopUpdateQueue;}// 用于判断当前函数是否是继承于 React.Component 的类组件Component.prototype.isReactComponent = {};Component.prototype.setState = function(partialState, callback) { // 类型校验 invariant( typeof partialState === 'object' || typeof partialState === 'function' || partialState == null, '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');};Component.prototype.forceUpdate = function(callback) { // 将强制更新操作入队 this.updater.enqueueForceUpdate(this, callback, 'forceUpdate');};
PureComponent
function PureComponent(props, context, updater) { // 与 Component 一致 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;}// 一个跟 Component 完全一致的类(没有初始化的内容)function ComponentDummy() {}ComponentDummy.prototype = Component.prototype;// 实现 PureComponent extends Componentconst pureComponentPrototype = (PureComponent.prototype = new ComponentDummy());pureComponentPrototype.constructor = PureComponent;// Avoid an extra prototype jump for these methods.Object.assign(pureComponentPrototype, Component.prototype);// 判断当前组件是否是继承于 React.PureComponent 的类组件pureComponentPrototype.isPureReactComponent = true;