UNPKG

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