1 | /**
|
2 | * Copyright 2013-present, Facebook, Inc.
|
3 | * All rights reserved.
|
4 | *
|
5 | * This source code is licensed under the BSD-style license found in the
|
6 | * LICENSE file in the root directory of this source tree. An additional grant
|
7 | * of patent rights can be found in the PATENTS file in the same directory.
|
8 | *
|
9 | */
|
10 |
|
11 | ;
|
12 |
|
13 | var _prodInvariant = require('./reactProdInvariant');
|
14 |
|
15 | var ReactNoopUpdateQueue = require('./ReactNoopUpdateQueue');
|
16 |
|
17 | var canDefineProperty = require('./canDefineProperty');
|
18 | var emptyObject = require('fbjs/lib/emptyObject');
|
19 | var invariant = require('fbjs/lib/invariant');
|
20 | var warning = require('fbjs/lib/warning');
|
21 |
|
22 | /**
|
23 | * Base class helpers for the updating state of a component.
|
24 | */
|
25 | function ReactComponent(props, context, updater) {
|
26 | this.props = props;
|
27 | this.context = context;
|
28 | this.refs = emptyObject;
|
29 | // We initialize the default updater but the real one gets injected by the
|
30 | // renderer.
|
31 | this.updater = updater || ReactNoopUpdateQueue;
|
32 | }
|
33 |
|
34 | ReactComponent.prototype.isReactComponent = {};
|
35 |
|
36 | /**
|
37 | * Sets a subset of the state. Always use this to mutate
|
38 | * state. You should treat `this.state` as immutable.
|
39 | *
|
40 | * There is no guarantee that `this.state` will be immediately updated, so
|
41 | * accessing `this.state` after calling this method may return the old value.
|
42 | *
|
43 | * There is no guarantee that calls to `setState` will run synchronously,
|
44 | * as they may eventually be batched together. You can provide an optional
|
45 | * callback that will be executed when the call to setState is actually
|
46 | * completed.
|
47 | *
|
48 | * When a function is provided to setState, it will be called at some point in
|
49 | * the future (not synchronously). It will be called with the up to date
|
50 | * component arguments (state, props, context). These values can be different
|
51 | * from this.* because your function may be called after receiveProps but before
|
52 | * shouldComponentUpdate, and this new state, props, and context will not yet be
|
53 | * assigned to this.
|
54 | *
|
55 | * @param {object|function} partialState Next partial state or function to
|
56 | * produce next partial state to be merged with current state.
|
57 | * @param {?function} callback Called after state is updated.
|
58 | * @final
|
59 | * @protected
|
60 | */
|
61 | ReactComponent.prototype.setState = function (partialState, callback) {
|
62 | !(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;
|
63 | this.updater.enqueueSetState(this, partialState);
|
64 | if (callback) {
|
65 | this.updater.enqueueCallback(this, callback, 'setState');
|
66 | }
|
67 | };
|
68 |
|
69 | /**
|
70 | * Forces an update. This should only be invoked when it is known with
|
71 | * certainty that we are **not** in a DOM transaction.
|
72 | *
|
73 | * You may want to call this when you know that some deeper aspect of the
|
74 | * component's state has changed but `setState` was not called.
|
75 | *
|
76 | * This will not invoke `shouldComponentUpdate`, but it will invoke
|
77 | * `componentWillUpdate` and `componentDidUpdate`.
|
78 | *
|
79 | * @param {?function} callback Called after update is complete.
|
80 | * @final
|
81 | * @protected
|
82 | */
|
83 | ReactComponent.prototype.forceUpdate = function (callback) {
|
84 | this.updater.enqueueForceUpdate(this);
|
85 | if (callback) {
|
86 | this.updater.enqueueCallback(this, callback, 'forceUpdate');
|
87 | }
|
88 | };
|
89 |
|
90 | /**
|
91 | * Deprecated APIs. These APIs used to exist on classic React classes but since
|
92 | * we would like to deprecate them, we're not going to move them over to this
|
93 | * modern base class. Instead, we define a getter that warns if it's accessed.
|
94 | */
|
95 | if (process.env.NODE_ENV !== 'production') {
|
96 | var deprecatedAPIs = {
|
97 | isMounted: ['isMounted', 'Instead, make sure to clean up subscriptions and pending requests in ' + 'componentWillUnmount to prevent memory leaks.'],
|
98 | replaceState: ['replaceState', 'Refactor your code to use setState instead (see ' + 'https://github.com/facebook/react/issues/3236).']
|
99 | };
|
100 | var defineDeprecationWarning = function (methodName, info) {
|
101 | if (canDefineProperty) {
|
102 | Object.defineProperty(ReactComponent.prototype, methodName, {
|
103 | get: function () {
|
104 | process.env.NODE_ENV !== 'production' ? warning(false, '%s(...) is deprecated in plain JavaScript React classes. %s', info[0], info[1]) : void 0;
|
105 | return undefined;
|
106 | }
|
107 | });
|
108 | }
|
109 | };
|
110 | for (var fnName in deprecatedAPIs) {
|
111 | if (deprecatedAPIs.hasOwnProperty(fnName)) {
|
112 | defineDeprecationWarning(fnName, deprecatedAPIs[fnName]);
|
113 | }
|
114 | }
|
115 | }
|
116 |
|
117 | module.exports = ReactComponent; |
\ | No newline at end of file |