@samitouri / QOS-React-2 / commits / 8de68c56f9

[Fiber] Remove unnecessary second getComponentName() (#9153)

* Remove unnecessary second getComponentName() We already have one. The other one is also less fragile and doesn't throw on null type. * Make setState in render warning check simpler This skips it earlier since in most cases phase won't be "render". This is unobservable.

Dan Abramov committed Mar 10, 2017 at 19:29 UTC 8de68c56f9fe9b9533d604853783be3e65a312fa
5 files changed +18 -25
src/renderers/shared/fiber/ReactChildFiber.js
+1 -1
@@ -40,7 +40,7 @@ var ReactCurrentOwner = require('react/lib/ReactCurrentOwner');
40
41 if (__DEV__) {
42 var { getCurrentFiberStackAddendum } = require('ReactDebugCurrentFiber');
43 - var { getComponentName } = require('ReactFiberTreeReflection');
43 + var getComponentName = require('getComponentName');
44 var warning = require('fbjs/lib/warning');
45 var didWarnAboutMaps = false;
46 }
src/renderers/shared/fiber/ReactFiberClassComponent.js
+3 -2
@@ -31,9 +31,10 @@ var {
31 beginUpdateQueue,
32 } = require('ReactFiberUpdateQueue');
33 var { hasContextChanged } = require('ReactFiberContext');
34 -var { getComponentName, isMounted } = require('ReactFiberTreeReflection');
34 +var { isMounted } = require('ReactFiberTreeReflection');
35 var ReactInstanceMap = require('ReactInstanceMap');
36 var emptyObject = require('fbjs/lib/emptyObject');
37 +var getComponentName = require('getComponentName');
38 var shallowEqual = require('fbjs/lib/shallowEqual');
39 var invariant = require('fbjs/lib/invariant');
40
@@ -119,7 +120,7 @@ module.exports = function(
120 shouldUpdate !== undefined,
121 '%s.shouldComponentUpdate(): Returned undefined instead of a ' +
122 'boolean value. Make sure to return true or false.',
122 - getComponentName(workInProgress)
123 + getComponentName(workInProgress) || 'Unknown'
124 );
125 }
126
src/renderers/shared/fiber/ReactFiberContext.js
+5 -5
@@ -16,10 +16,10 @@ import type { Fiber } from 'ReactFiber';
16 import type { StackCursor } from 'ReactFiberStack';
17
18 var emptyObject = require('fbjs/lib/emptyObject');
19 +var getComponentName = require('getComponentName');
20 var invariant = require('fbjs/lib/invariant');
21 var warning = require('fbjs/lib/warning');
22 var {
22 - getComponentName,
23 isFiberMounted,
24 } = require('ReactFiberTreeReflection');
25 var {
@@ -96,7 +96,7 @@ exports.getMaskedContext = function(workInProgress : Fiber, unmaskedContext : Ob
96 }
97
98 if (__DEV__) {
99 - const name = getComponentName(workInProgress);
99 + const name = getComponentName(workInProgress) || 'Unknown';
100 ReactDebugCurrentFrame.current = workInProgress;
101 checkReactTypeSpec(contextTypes, context, 'context', name);
102 ReactDebugCurrentFrame.current = null;
@@ -156,7 +156,7 @@ function processChildContext(fiber : Fiber, parentContext : Object, isReconcilin
156 // It has only been added in Fiber to match the (unintentional) behavior in Stack.
157 if (typeof instance.getChildContext !== 'function') {
158 if (__DEV__) {
159 - const componentName = getComponentName(fiber);
159 + const componentName = getComponentName(fiber) || 'Unknown';
160
161 if (!warnedAboutMissingGetChildContext[componentName]) {
162 warnedAboutMissingGetChildContext[componentName] = true;
@@ -187,12 +187,12 @@ function processChildContext(fiber : Fiber, parentContext : Object, isReconcilin
187 invariant(
188 contextKey in childContextTypes,
189 '%s.getChildContext(): key "%s" is not defined in childContextTypes.',
190 - getComponentName(fiber),
190 + getComponentName(fiber) || 'Unknown',
191 contextKey
192 );
193 }
194 if (__DEV__) {
195 - const name = getComponentName(fiber);
195 + const name = getComponentName(fiber) || 'Unknown';
196 // We can only provide accurate element stacks if we pass work-in-progress tree
197 // during the begin or complete phase. However currently this function is also
198 // called from unstable_renderSubtree legacy implementation. In this case it unsafe to
src/renderers/shared/fiber/ReactFiberReconciler.js
+7 -4
@@ -33,7 +33,7 @@ if (__DEV__) {
33 var warning = require('fbjs/lib/warning');
34 var ReactFiberInstrumentation = require('ReactFiberInstrumentation');
35 var ReactDebugCurrentFiber = require('ReactDebugCurrentFiber');
36 - var { getComponentName } = require('ReactFiberTreeReflection');
36 + var getComponentName = require('getComponentName');
37 }
38
39 var { findCurrentHostFiber } = require('ReactFiberTreeReflection');
@@ -148,14 +148,17 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
148
149 function scheduleTopLevelUpdate(current : Fiber, element : ReactNodeList, callback : ?Function) {
150 if (__DEV__) {
151 - if (ReactDebugCurrentFiber.current !== null) {
151 + if (
152 + ReactDebugCurrentFiber.phase === 'render' &&
153 + ReactDebugCurrentFiber.current !== null
154 + ) {
155 warning(
153 - ReactDebugCurrentFiber.phase !== 'render',
156 + false,
157 'Render methods should be a pure function of props and state; ' +
158 'triggering nested component updates from render is not allowed. ' +
159 'If necessary, trigger nested updates in componentDidUpdate.\n\n' +
160 'Check the render method of %s.',
158 - getComponentName(ReactDebugCurrentFiber.current)
161 + getComponentName(ReactDebugCurrentFiber.current) || 'Unknown'
162 );
163 }
164 }
src/renderers/shared/fiber/ReactFiberTreeReflection.js
+2 -13
@@ -17,6 +17,7 @@ import type { Fiber } from 'ReactFiber';
17 var ReactInstanceMap = require('ReactInstanceMap');
18 var ReactCurrentOwner = require('react/lib/ReactCurrentOwner');
19
20 +var getComponentName = require('getComponentName');
21 var invariant = require('fbjs/lib/invariant');
22
23 if (__DEV__) {
@@ -84,7 +85,7 @@ exports.isMounted = function(component : ReactComponent<any, any, any>) : boolea
85 'never access something that requires stale data from the previous ' +
86 'render, such as refs. Move this logic to componentDidMount and ' +
87 'componentDidUpdate instead.',
87 - getComponentName(ownerFiber)
88 + getComponentName(ownerFiber) || 'A component'
89 );
90 instance._warnedAboutRefsInRender = true;
91 }
@@ -266,15 +267,3 @@ exports.findCurrentHostFiber = function(parent : Fiber) : Fiber | null {
267 // eslint-disable-next-line no-unreachable
268 return null;
269 };
269 -
270 -function getComponentName(fiber: Fiber): string {
271 - const type = fiber.type;
272 - const instance = fiber.stateNode;
273 - const constructor = instance && instance.constructor;
274 - return (
275 - type.displayName || (constructor && constructor.displayName) ||
276 - type.name || (constructor && constructor.name) ||
277 - 'A Component'
278 - );
279 -}
280 -exports.getComponentName = getComponentName;