main
js 110 lines 3.65 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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 const didWarnStateUpdateForUnmountedComponent = {};
9
10 function warnNoop(publicInstance, callerName) {
11 if (__DEV__) {
12 const constructor = publicInstance.constructor;
13 const componentName =
14 (constructor && (constructor.displayName || constructor.name)) ||
15 'ReactClass';
16 const warningKey = `${componentName}.${callerName}`;
17 if (didWarnStateUpdateForUnmountedComponent[warningKey]) {
18 return;
19 }
20 console.error(
21 "Can't call %s on a component that is not yet mounted. " +
22 'This is a no-op, but it might indicate a bug in your application. ' +
23 'Instead, assign to `this.state` directly or define a `state = {};` ' +
24 'class property with the desired state in the %s component.',
25 callerName,
26 componentName,
27 );
28 didWarnStateUpdateForUnmountedComponent[warningKey] = true;
29 }
30 }
31
32 /**
33 * This is the abstract API for an update queue.
34 */
35 const ReactNoopUpdateQueue = {
36 /**
37 * Checks whether or not this composite component is mounted.
38 * @param {ReactClass} publicInstance The instance we want to test.
39 * @return {boolean} True if mounted, false otherwise.
40 * @protected
41 * @final
42 */
43 isMounted: function (publicInstance) {
44 return false;
45 },
46
47 /**
48 * Forces an update. This should only be invoked when it is known with
49 * certainty that we are **not** in a DOM transaction.
50 *
51 * You may want to call this when you know that some deeper aspect of the
52 * component's state has changed but `setState` was not called.
53 *
54 * This will not invoke `shouldComponentUpdate`, but it will invoke
55 * `componentWillUpdate` and `componentDidUpdate`.
56 *
57 * @param {ReactClass} publicInstance The instance that should rerender.
58 * @param {?function} callback Called after component is updated.
59 * @param {?string} callerName name of the calling function in the public API.
60 * @internal
61 */
62 enqueueForceUpdate: function (publicInstance, callback, callerName) {
63 warnNoop(publicInstance, 'forceUpdate');
64 },
65
66 /**
67 * Replaces all of the state. Always use this or `setState` to mutate state.
68 * You should treat `this.state` as immutable.
69 *
70 * There is no guarantee that `this.state` will be immediately updated, so
71 * accessing `this.state` after calling this method may return the old value.
72 *
73 * @param {ReactClass} publicInstance The instance that should rerender.
74 * @param {object} completeState Next state.
75 * @param {?function} callback Called after component is updated.
76 * @param {?string} callerName name of the calling function in the public API.
77 * @internal
78 */
79 enqueueReplaceState: function (
80 publicInstance,
81 completeState,
82 callback,
83 callerName,
84 ) {
85 warnNoop(publicInstance, 'replaceState');
86 },
87
88 /**
89 * Sets a subset of the state. This only exists because _pendingState is
90 * internal. This provides a merging strategy that is not available to deep
91 * properties which is confusing. TODO: Expose pendingState or don't use it
92 * during the merge.
93 *
94 * @param {ReactClass} publicInstance The instance that should rerender.
95 * @param {object} partialState Next partial state to be merged with state.
96 * @param {?function} callback Called after component is updated.
97 * @param {?string} Name of the calling function in the public API.
98 * @internal
99 */
100 enqueueSetState: function (
101 publicInstance,
102 partialState,
103 callback,
104 callerName,
105 ) {
106 warnNoop(publicInstance, 'setState');
107 },
108 };
109
110 export default ReactNoopUpdateQueue;