Use ReactDebugLifeCycle to track if we're inside getChildContext
Andrew Clark committed
Feb 22, 2017 at 14:02 UTC
d2c28be69b62cf1fbb1198e55ca8dd72ccb1260a
3 files changed
+28
-43
src/renderers/shared/fiber/ReactFiberContext.js
+11
-24
@@ -35,6 +35,7 @@ const {
35
if (__DEV__) {
36
var checkReactTypeSpec = require('checkReactTypeSpec');
37
var ReactDebugCurrentFrame = require('ReactDebugCurrentFrame');
38
+ var ReactDebugLifeCycle = require('ReactDebugLifeCycle');
39
var warnedAboutMissingGetChildContext = {};
40
}
41
@@ -47,21 +48,6 @@ let didPerformWorkStackCursor : StackCursor<boolean> = createCursor(false);
48
// pushed the next context provider, and now need to merge their contexts.
49
let previousContext : Object = emptyObject;
50
50
-if (__DEV__) {
51
- var _isProcessingChildContext = false;
52
- var onBeginProcessingChildContext = function() {
53
- _isProcessingChildContext = true;
54
- };
55
- exports.onBeginProcessingChildContext = onBeginProcessingChildContext;
56
- var onEndProcessingChildContext = function() {
57
- _isProcessingChildContext = false;
58
- };
59
- exports.onEndProcessingChildContext = onEndProcessingChildContext;
60
- exports.isProcessingChildContext = function() : boolean {
61
- return _isProcessingChildContext;
62
- };
63
-}
64
-
51
function getUnmaskedContext(workInProgress : Fiber) : Object {
52
const hasOwnContext = isContextProvider(workInProgress);
53
if (hasOwnContext) {
@@ -159,10 +145,6 @@ exports.pushTopLevelContextObject = function(fiber : Fiber, context : Object, di
145
};
146
147
function processChildContext(fiber : Fiber, parentContext : Object, isReconciling : boolean): Object {
162
- if (__DEV__) {
163
- onBeginProcessingChildContext();
164
- }
165
-
148
const instance = fiber.stateNode;
149
const childContextTypes = fiber.type.childContextTypes;
150
@@ -187,7 +169,16 @@ function processChildContext(fiber : Fiber, parentContext : Object, isReconcilin
169
return parentContext;
170
}
171
190
- const childContext = instance.getChildContext();
172
+ let childContext;
173
+ if (__DEV__) {
174
+ ReactDebugLifeCycle.current = fiber;
175
+ ReactDebugLifeCycle.phase = 'getChildContext';
176
+ childContext = instance.getChildContext();
177
+ ReactDebugLifeCycle.current = null;
178
+ ReactDebugLifeCycle.phase = null;
179
+ } else {
180
+ childContext = instance.getChildContext();
181
+ }
182
for (let contextKey in childContext) {
183
invariant(
184
contextKey in childContextTypes,
@@ -209,10 +200,6 @@ function processChildContext(fiber : Fiber, parentContext : Object, isReconcilin
200
ReactDebugCurrentFrame.current = null;
201
}
202
212
- if (__DEV__) {
213
- onEndProcessingChildContext();
214
- }
215
-
203
return {...parentContext, ...childContext};
204
}
205
exports.processChildContext = processChildContext;
src/renderers/shared/fiber/ReactFiberScheduler.js
+16
-18
@@ -91,10 +91,6 @@ if (__DEV__) {
91
var ReactFiberInstrumentation = require('ReactFiberInstrumentation');
92
var ReactDebugCurrentFiber = require('ReactDebugCurrentFiber');
93
var ReactDebugLifeCycle = require('ReactDebugLifeCycle');
94
- var {
95
- isProcessingChildContext,
96
- onEndProcessingChildContext,
97
- } = require('ReactFiberContext');
94
95
var warnAboutUpdateOnUnmounted = function(instance : ReactClass<any>) {
96
const ctor = instance.constructor;
@@ -109,19 +105,22 @@ if (__DEV__) {
105
};
106
107
var warnAboutInvalidUpdates = function(instance : ReactClass<any>) {
112
- if (isProcessingChildContext()) {
113
- warning(
114
- false,
115
- 'setState(...): Cannot call setState() inside getChildContext()',
116
- );
117
- } else if (ReactDebugLifeCycle.phase === 'render') {
118
- warning(
119
- false,
120
- 'Cannot update during an existing state transition (such as within ' +
121
- '`render` or another component\'s constructor). Render methods should ' +
122
- 'be a pure function of props and state; constructor side-effects are ' +
123
- 'an anti-pattern, but can be moved to `componentWillMount`.'
124
- );
108
+ switch (ReactDebugLifeCycle.phase) {
109
+ case 'getChildContext':
110
+ warning(
111
+ false,
112
+ 'setState(...): Cannot call setState() inside getChildContext()',
113
+ );
114
+ break;
115
+ case 'render':
116
+ warning(
117
+ false,
118
+ 'Cannot update during an existing state transition (such as within ' +
119
+ '`render` or another component\'s constructor). Render methods should ' +
120
+ 'be a pure function of props and state; constructor side-effects are ' +
121
+ 'an anti-pattern, but can be moved to `componentWillMount`.'
122
+ );
123
+ break;
124
}
125
};
126
}
@@ -883,7 +882,6 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(config : HostConfig<T, P,
882
ReactDebugCurrentFiber.current = null;
883
ReactDebugLifeCycle.current = null;
884
ReactDebugLifeCycle.phase = null;
886
- onEndProcessingChildContext();
885
}
886
// It is no longer valid because this unit of work failed.
887
nextUnitOfWork = null;
src/renderers/shared/fiber/isomorphic/ReactDebugLifeCycle.js
+1
-1
@@ -14,7 +14,7 @@
14
15
import type { Fiber } from 'ReactFiber';
16
17
-type LifeCyclePhase = 'render';
17
+type LifeCyclePhase = 'render' | 'getChildContext';
18
19
const ReactDebugLifeCycle = {
20
current: (null : Fiber | null),