117 lines
4.5 KiB
JavaScript
117 lines
4.5 KiB
JavaScript
|
/**
|
||
|
* Copyright 2013-present, Facebook, Inc.
|
||
|
* All rights reserved.
|
||
|
*
|
||
|
* This source code is licensed under the BSD-style license found in the
|
||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
'use strict';
|
||
|
|
||
|
var _prodInvariant = require('./reactProdInvariant');
|
||
|
|
||
|
var ReactNoopUpdateQueue = require('./ReactNoopUpdateQueue');
|
||
|
|
||
|
var canDefineProperty = require('./canDefineProperty');
|
||
|
var emptyObject = require('fbjs/lib/emptyObject');
|
||
|
var invariant = require('fbjs/lib/invariant');
|
||
|
var warning = require('fbjs/lib/warning');
|
||
|
|
||
|
/**
|
||
|
* Base class helpers for the updating state of a component.
|
||
|
*/
|
||
|
function ReactComponent(props, context, updater) {
|
||
|
this.props = props;
|
||
|
this.context = context;
|
||
|
this.refs = emptyObject;
|
||
|
// We initialize the default updater but the real one gets injected by the
|
||
|
// renderer.
|
||
|
this.updater = updater || ReactNoopUpdateQueue;
|
||
|
}
|
||
|
|
||
|
ReactComponent.prototype.isReactComponent = {};
|
||
|
|
||
|
/**
|
||
|
* Sets a subset of the state. Always use this to mutate
|
||
|
* state. You should treat `this.state` as immutable.
|
||
|
*
|
||
|
* There is no guarantee that `this.state` will be immediately updated, so
|
||
|
* accessing `this.state` after calling this method may return the old value.
|
||
|
*
|
||
|
* There is no guarantee that calls to `setState` will run synchronously,
|
||
|
* as they may eventually be batched together. You can provide an optional
|
||
|
* callback that will be executed when the call to setState is actually
|
||
|
* completed.
|
||
|
*
|
||
|
* When a function is provided to setState, it will be called at some point in
|
||
|
* the future (not synchronously). It will be called with the up to date
|
||
|
* component arguments (state, props, context). These values can be different
|
||
|
* from this.* because your function may be called after receiveProps but before
|
||
|
* shouldComponentUpdate, and this new state, props, and context will not yet be
|
||
|
* assigned to this.
|
||
|
*
|
||
|
* @param {object|function} partialState Next partial state or function to
|
||
|
* produce next partial state to be merged with current state.
|
||
|
* @param {?function} callback Called after state is updated.
|
||
|
* @final
|
||
|
* @protected
|
||
|
*/
|
||
|
ReactComponent.prototype.setState = function (partialState, callback) {
|
||
|
!(typeof partialState === 'object' || typeof partialState === 'function' || partialState == null) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'setState(...): takes an object of state variables to update or a function which returns an object of state variables.') : _prodInvariant('85') : void 0;
|
||
|
this.updater.enqueueSetState(this, partialState);
|
||
|
if (callback) {
|
||
|
this.updater.enqueueCallback(this, callback, 'setState');
|
||
|
}
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Forces an update. This should only be invoked when it is known with
|
||
|
* certainty that we are **not** in a DOM transaction.
|
||
|
*
|
||
|
* You may want to call this when you know that some deeper aspect of the
|
||
|
* component's state has changed but `setState` was not called.
|
||
|
*
|
||
|
* This will not invoke `shouldComponentUpdate`, but it will invoke
|
||
|
* `componentWillUpdate` and `componentDidUpdate`.
|
||
|
*
|
||
|
* @param {?function} callback Called after update is complete.
|
||
|
* @final
|
||
|
* @protected
|
||
|
*/
|
||
|
ReactComponent.prototype.forceUpdate = function (callback) {
|
||
|
this.updater.enqueueForceUpdate(this);
|
||
|
if (callback) {
|
||
|
this.updater.enqueueCallback(this, callback, 'forceUpdate');
|
||
|
}
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Deprecated APIs. These APIs used to exist on classic React classes but since
|
||
|
* we would like to deprecate them, we're not going to move them over to this
|
||
|
* modern base class. Instead, we define a getter that warns if it's accessed.
|
||
|
*/
|
||
|
if (process.env.NODE_ENV !== 'production') {
|
||
|
var deprecatedAPIs = {
|
||
|
isMounted: ['isMounted', 'Instead, make sure to clean up subscriptions and pending requests in ' + 'componentWillUnmount to prevent memory leaks.'],
|
||
|
replaceState: ['replaceState', 'Refactor your code to use setState instead (see ' + 'https://github.com/facebook/react/issues/3236).']
|
||
|
};
|
||
|
var defineDeprecationWarning = function (methodName, info) {
|
||
|
if (canDefineProperty) {
|
||
|
Object.defineProperty(ReactComponent.prototype, methodName, {
|
||
|
get: function () {
|
||
|
process.env.NODE_ENV !== 'production' ? warning(false, '%s(...) is deprecated in plain JavaScript React classes. %s', info[0], info[1]) : void 0;
|
||
|
return undefined;
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
};
|
||
|
for (var fnName in deprecatedAPIs) {
|
||
|
if (deprecatedAPIs.hasOwnProperty(fnName)) {
|
||
|
defineDeprecationWarning(fnName, deprecatedAPIs[fnName]);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = ReactComponent;
|