Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80517 views
1
/**
2
* Copyright 2013-2015, 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
* @providesModule ReactComponent
10
*/
11
12
'use strict';
13
14
var ReactUpdateQueue = require("./ReactUpdateQueue");
15
16
var invariant = require("./invariant");
17
var warning = require("./warning");
18
19
/**
20
* Base class helpers for the updating state of a component.
21
*/
22
function ReactComponent(props, context) {
23
this.props = props;
24
this.context = context;
25
}
26
27
/**
28
* Sets a subset of the state. Always use this to mutate
29
* state. You should treat `this.state` as immutable.
30
*
31
* There is no guarantee that `this.state` will be immediately updated, so
32
* accessing `this.state` after calling this method may return the old value.
33
*
34
* There is no guarantee that calls to `setState` will run synchronously,
35
* as they may eventually be batched together. You can provide an optional
36
* callback that will be executed when the call to setState is actually
37
* completed.
38
*
39
* When a function is provided to setState, it will be called at some point in
40
* the future (not synchronously). It will be called with the up to date
41
* component arguments (state, props, context). These values can be different
42
* from this.* because your function may be called after receiveProps but before
43
* shouldComponentUpdate, and this new state, props, and context will not yet be
44
* assigned to this.
45
*
46
* @param {object|function} partialState Next partial state or function to
47
* produce next partial state to be merged with current state.
48
* @param {?function} callback Called after state is updated.
49
* @final
50
* @protected
51
*/
52
ReactComponent.prototype.setState = function(partialState, callback) {
53
("production" !== process.env.NODE_ENV ? invariant(
54
typeof partialState === 'object' ||
55
typeof partialState === 'function' ||
56
partialState == null,
57
'setState(...): takes an object of state variables to update or a ' +
58
'function which returns an object of state variables.'
59
) : invariant(typeof partialState === 'object' ||
60
typeof partialState === 'function' ||
61
partialState == null));
62
if ("production" !== process.env.NODE_ENV) {
63
("production" !== process.env.NODE_ENV ? warning(
64
partialState != null,
65
'setState(...): You passed an undefined or null state object; ' +
66
'instead, use forceUpdate().'
67
) : null);
68
}
69
ReactUpdateQueue.enqueueSetState(this, partialState);
70
if (callback) {
71
ReactUpdateQueue.enqueueCallback(this, callback);
72
}
73
};
74
75
/**
76
* Forces an update. This should only be invoked when it is known with
77
* certainty that we are **not** in a DOM transaction.
78
*
79
* You may want to call this when you know that some deeper aspect of the
80
* component's state has changed but `setState` was not called.
81
*
82
* This will not invoke `shouldComponentUpdate`, but it will invoke
83
* `componentWillUpdate` and `componentDidUpdate`.
84
*
85
* @param {?function} callback Called after update is complete.
86
* @final
87
* @protected
88
*/
89
ReactComponent.prototype.forceUpdate = function(callback) {
90
ReactUpdateQueue.enqueueForceUpdate(this);
91
if (callback) {
92
ReactUpdateQueue.enqueueCallback(this, callback);
93
}
94
};
95
96
/**
97
* Deprecated APIs. These APIs used to exist on classic React classes but since
98
* we would like to deprecate them, we're not going to move them over to this
99
* modern base class. Instead, we define a getter that warns if it's accessed.
100
*/
101
if ("production" !== process.env.NODE_ENV) {
102
var deprecatedAPIs = {
103
getDOMNode: [
104
'getDOMNode',
105
'Use React.findDOMNode(component) instead.'
106
],
107
isMounted: [
108
'isMounted',
109
'Instead, make sure to clean up subscriptions and pending requests in ' +
110
'componentWillUnmount to prevent memory leaks.'
111
],
112
replaceProps: [
113
'replaceProps',
114
'Instead, call React.render again at the top level.'
115
],
116
replaceState: [
117
'replaceState',
118
'Refactor your code to use setState instead (see ' +
119
'https://github.com/facebook/react/issues/3236).'
120
],
121
setProps: [
122
'setProps',
123
'Instead, call React.render again at the top level.'
124
]
125
};
126
var defineDeprecationWarning = function(methodName, info) {
127
try {
128
Object.defineProperty(ReactComponent.prototype, methodName, {
129
get: function() {
130
("production" !== process.env.NODE_ENV ? warning(
131
false,
132
'%s(...) is deprecated in plain JavaScript React classes. %s',
133
info[0],
134
info[1]
135
) : null);
136
return undefined;
137
}
138
});
139
} catch (x) {
140
// IE will fail on defineProperty (es5-shim/sham too)
141
}
142
};
143
for (var fnName in deprecatedAPIs) {
144
if (deprecatedAPIs.hasOwnProperty(fnName)) {
145
defineDeprecationWarning(fnName, deprecatedAPIs[fnName]);
146
}
147
}
148
}
149
150
module.exports = ReactComponent;
151
152