Source address: packages/react/SRC/ReactBaseClasses. Js

Making the address

* Base class helpers for the updating state of a component. * equivalent to Es6 * class component {* constructor(context, props, updater) { * this.props = props this.context = context this.refs = emptyObject this.updater = updater || ReactNoopUpdateQueue * } * } * */ function Component(props, context, updater) { this.props = props; this.context = context; // If a component has a string reference, we will assign a different object this.refs = emptyObject; // We initialize the default updater but the real one gets injected by the // renderer. this.updater = updater || ReactNoopUpdateQueue; } / / user judgment is React.Com ponent Component. The prototype. IsReactComponent = {}; /** * @param {object|function} partialState * @param {? function} callback Called after state is updated. * @final * @protected * setState({ * * }, Callbck) * / / / in mount Component prototype setState Component. The prototype. The 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.', ); / / execution setState enclosing updater. EnqueueSetState (this, partialState, callback, 'setState); }; /** * @param {? Callback Called after update is complete. * @final * @protected */ / Mount forceUpdate on the prototype Component. The prototype. ForceUpdate = function (the callback) {/ / execution forceUpdate enclosing updater. EnqueueForceUpdate (this callback, 'forceUpdate'); };Copy the code

PureComponent

function ComponentDummy() {} ComponentDummy.prototype = Component.prototype; /** * PureComponent, Class PureComponent {* constructor(context, updater) {* this. Props = props; * this.context = context; * this.refs = emptyObject; * this.updater = updater || ReactNoopUpdateQqueue; * } * } */ function PureComponent(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; } // The PureComponent prototype points to an empty object ComponentDummy, Const pureComponentPrototype = (pureComponent.prototype = new ComponentDummy()); pureComponentPrototype.constructor = PureComponent; // Avoid these methods' extra prototype jump object. assign(pureComponentPrototype, component.prototype); pureComponentPrototype.isPureReactComponent = true; export {Component, PureComponent};Copy the code