Skip to content

Commit 7df6572

Browse files
author
Brian Vaughn
authored
Split getComponentName into getComponentNameFromFiber and getComponentNameFromType (#20940)
Split getComponentName into getComponentNameFromFiber and getComponentNameFromType
1 parent ee43263 commit 7df6572

40 files changed

+386
-308
lines changed

packages/react-dom/src/client/ReactDOMLegacy.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
findHostInstance,
3333
findHostInstanceWithWarning,
3434
} from 'react-reconciler/src/ReactFiberReconciler';
35-
import getComponentName from 'shared/getComponentName';
35+
import getComponentNameFromType from 'shared/getComponentNameFromType';
3636
import invariant from 'shared/invariant';
3737
import ReactSharedInternals from 'shared/ReactSharedInternals';
3838
import {has as hasInstance} from 'shared/ReactInstanceMap';
@@ -235,7 +235,7 @@ export function findDOMNode(
235235
'never access something that requires stale data from the previous ' +
236236
'render, such as refs. Move this logic to componentDidMount and ' +
237237
'componentDidUpdate instead.',
238-
getComponentName(owner.type) || 'A component',
238+
getComponentNameFromType(owner.type) || 'A component',
239239
);
240240
}
241241
owner.stateNode._warnedAboutRefsInRender = true;

packages/react-dom/src/server/ReactPartialRenderer.js

+17-12
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type {ReactProvider, ReactContext} from 'shared/ReactTypes';
1414

1515
import * as React from 'react';
1616
import invariant from 'shared/invariant';
17-
import getComponentName from 'shared/getComponentName';
17+
import getComponentNameFromType from 'shared/getComponentNameFromType';
1818
import {describeUnknownElementTypeFrameInDEV} from 'shared/ReactComponentStackFrame';
1919
import ReactSharedInternals from 'shared/ReactSharedInternals';
2020
import {
@@ -259,7 +259,7 @@ function warnNoop(
259259
if (__DEV__) {
260260
const constructor = publicInstance.constructor;
261261
const componentName =
262-
(constructor && getComponentName(constructor)) || 'ReactClass';
262+
(constructor && getComponentNameFromType(constructor)) || 'ReactClass';
263263
const warningKey = componentName + '.' + callerName;
264264
if (didWarnAboutNoopUpdateForComponent[warningKey]) {
265265
return;
@@ -404,7 +404,7 @@ function validateRenderResult(child, type) {
404404
'%s(...): Nothing was returned from render. This usually means a ' +
405405
'return statement is missing. Or, to render nothing, ' +
406406
'return null.',
407-
getComponentName(type) || 'Component',
407+
getComponentNameFromType(type) || 'Component',
408408
);
409409
}
410410
}
@@ -467,7 +467,8 @@ function resolve(
467467
if (typeof Component.getDerivedStateFromProps === 'function') {
468468
if (__DEV__) {
469469
if (inst.state === null || inst.state === undefined) {
470-
const componentName = getComponentName(Component) || 'Unknown';
470+
const componentName =
471+
getComponentNameFromType(Component) || 'Unknown';
471472
if (!didWarnAboutUninitializedState[componentName]) {
472473
console.error(
473474
'`%s` uses `getDerivedStateFromProps` but its initial state is ' +
@@ -491,7 +492,8 @@ function resolve(
491492

492493
if (__DEV__) {
493494
if (partialState === undefined) {
494-
const componentName = getComponentName(Component) || 'Unknown';
495+
const componentName =
496+
getComponentNameFromType(Component) || 'Unknown';
495497
if (!didWarnAboutUndefinedDerivedState[componentName]) {
496498
console.error(
497499
'%s.getDerivedStateFromProps(): A valid state object (or null) must be returned. ' +
@@ -513,7 +515,8 @@ function resolve(
513515
Component.prototype &&
514516
typeof Component.prototype.render === 'function'
515517
) {
516-
const componentName = getComponentName(Component) || 'Unknown';
518+
const componentName =
519+
getComponentNameFromType(Component) || 'Unknown';
517520

518521
if (!didWarnAboutBadClass[componentName]) {
519522
console.error(
@@ -535,7 +538,8 @@ function resolve(
535538
// Support for module components is deprecated and is removed behind a flag.
536539
// Whether or not it would crash later, we want to show a good message in DEV first.
537540
if (inst != null && inst.render != null) {
538-
const componentName = getComponentName(Component) || 'Unknown';
541+
const componentName =
542+
getComponentNameFromType(Component) || 'Unknown';
539543
if (!didWarnAboutModulePatternComponent[componentName]) {
540544
console.error(
541545
'The <%s /> component appears to be a function component that returns a class instance. ' +
@@ -583,7 +587,8 @@ function resolve(
583587
warnAboutDeprecatedLifecycles &&
584588
inst.componentWillMount.__suppressDeprecationWarning !== true
585589
) {
586-
const componentName = getComponentName(Component) || 'Unknown';
590+
const componentName =
591+
getComponentNameFromType(Component) || 'Unknown';
587592

588593
if (!didWarnAboutDeprecatedWillMount[componentName]) {
589594
console.warn(
@@ -665,7 +670,7 @@ function resolve(
665670
console.error(
666671
'%s uses the legacy childContextTypes API which is no longer supported. ' +
667672
'Use React.createContext() instead.',
668-
getComponentName(Component) || 'Unknown',
673+
getComponentNameFromType(Component) || 'Unknown',
669674
);
670675
}
671676
}
@@ -678,7 +683,7 @@ function resolve(
678683
invariant(
679684
contextKey in childContextTypes,
680685
'%s.getChildContext(): key "%s" is not defined in childContextTypes.',
681-
getComponentName(Component) || 'Unknown',
686+
getComponentNameFromType(Component) || 'Unknown',
682687
contextKey,
683688
);
684689
}
@@ -687,7 +692,7 @@ function resolve(
687692
console.error(
688693
'%s.getChildContext(): childContextTypes must be defined in order to ' +
689694
'use getChildContext().',
690-
getComponentName(Component) || 'Unknown',
695+
getComponentNameFromType(Component) || 'Unknown',
691696
);
692697
}
693698
}
@@ -1298,7 +1303,7 @@ class ReactDOMServerRenderer {
12981303
"it's defined in, or you might have mixed up default and " +
12991304
'named imports.';
13001305
}
1301-
const ownerName = owner ? getComponentName(owner) : null;
1306+
const ownerName = owner ? getComponentNameFromType(owner) : null;
13021307
if (ownerName) {
13031308
info += '\n\nCheck the render method of `' + ownerName + '`.';
13041309
}

packages/react-dom/src/server/ReactPartialRendererContext.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {ReactContext} from 'shared/ReactTypes';
1212

1313
import {disableLegacyContext} from 'shared/ReactFeatureFlags';
1414
import {REACT_CONTEXT_TYPE, REACT_PROVIDER_TYPE} from 'shared/ReactSymbols';
15-
import getComponentName from 'shared/getComponentName';
15+
import getComponentNameFromType from 'shared/getComponentNameFromType';
1616
import checkPropTypes from 'shared/checkPropTypes';
1717

1818
let didWarnAboutInvalidateContextType;
@@ -105,7 +105,7 @@ export function processContext(
105105
console.error(
106106
'%s defines an invalid contextType. ' +
107107
'contextType should point to the Context object returned by React.createContext().%s',
108-
getComponentName(type) || 'Component',
108+
getComponentNameFromType(type) || 'Component',
109109
addendum,
110110
);
111111
}
@@ -121,7 +121,7 @@ export function processContext(
121121
console.error(
122122
'%s uses the legacy contextTypes API which is no longer supported. ' +
123123
'Use React.createContext() with static contextType instead.',
124-
getComponentName(type) || 'Unknown',
124+
getComponentNameFromType(type) || 'Unknown',
125125
);
126126
}
127127
}
@@ -142,7 +142,7 @@ export function processContext(
142142
console.error(
143143
'%s uses the legacy contextTypes API which is no longer supported. ' +
144144
'Use React.createContext() with React.useContext() instead.',
145-
getComponentName(type) || 'Unknown',
145+
getComponentNameFromType(type) || 'Unknown',
146146
);
147147
}
148148
}

packages/react-native-renderer/src/ReactFabric.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import {
4343
} from './ReactNativeFiberInspector';
4444
import {LegacyRoot} from 'react-reconciler/src/ReactRootTags';
4545
import ReactSharedInternals from 'shared/ReactSharedInternals';
46-
import getComponentName from 'shared/getComponentName';
46+
import getComponentNameFromType from 'shared/getComponentNameFromType';
4747

4848
const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
4949

@@ -60,7 +60,7 @@ function findHostInstance_DEPRECATED<TElementType: ElementType>(
6060
'never access something that requires stale data from the previous ' +
6161
'render, such as refs. Move this logic to componentDidMount and ' +
6262
'componentDidUpdate instead.',
63-
getComponentName(owner.type) || 'A component',
63+
getComponentNameFromType(owner.type) || 'A component',
6464
);
6565
}
6666

@@ -111,7 +111,7 @@ function findNodeHandle(componentOrHandle: any): ?number {
111111
'never access something that requires stale data from the previous ' +
112112
'render, such as refs. Move this logic to componentDidMount and ' +
113113
'componentDidUpdate instead.',
114-
getComponentName(owner.type) || 'A component',
114+
getComponentNameFromType(owner.type) || 'A component',
115115
);
116116
}
117117

packages/react-native-renderer/src/ReactNativeFiberInspector.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
findCurrentHostFiber,
1515
findCurrentFiberUsingSlowPath,
1616
} from 'react-reconciler/src/ReactFiberTreeReflection';
17-
import getComponentName from 'shared/getComponentName';
17+
import getComponentNameFromType from 'shared/getComponentNameFromType';
1818
import {HostComponent} from 'react-reconciler/src/ReactWorkTags';
1919
import invariant from 'shared/invariant';
2020
// Module provided by RN:
@@ -81,7 +81,7 @@ if (__DEV__) {
8181

8282
const createHierarchy = function(fiberHierarchy) {
8383
return fiberHierarchy.map(fiber => ({
84-
name: getComponentName(fiber.type),
84+
name: getComponentNameFromType(fiber.type),
8585
getInspectorData: findNodeHandle => {
8686
return {
8787
props: getHostProps(fiber),

packages/react-native-renderer/src/ReactNativeRenderer.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import {
4646
} from './ReactNativeFiberInspector';
4747
import {LegacyRoot} from 'react-reconciler/src/ReactRootTags';
4848
import ReactSharedInternals from 'shared/ReactSharedInternals';
49-
import getComponentName from 'shared/getComponentName';
49+
import getComponentNameFromType from 'shared/getComponentNameFromType';
5050

5151
const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
5252

@@ -63,7 +63,7 @@ function findHostInstance_DEPRECATED(
6363
'never access something that requires stale data from the previous ' +
6464
'render, such as refs. Move this logic to componentDidMount and ' +
6565
'componentDidUpdate instead.',
66-
getComponentName(owner.type) || 'A component',
66+
getComponentNameFromType(owner.type) || 'A component',
6767
);
6868
}
6969

@@ -110,7 +110,7 @@ function findNodeHandle(componentOrHandle: any): ?number {
110110
'never access something that requires stale data from the previous ' +
111111
'render, such as refs. Move this logic to componentDidMount and ' +
112112
'componentDidUpdate instead.',
113-
getComponentName(owner.type) || 'A component',
113+
getComponentNameFromType(owner.type) || 'A component',
114114
);
115115
}
116116

packages/react-reconciler/src/ReactChildFiber.new.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {ReactPortal} from 'shared/ReactTypes';
1212
import type {Fiber} from './ReactInternalTypes';
1313
import type {Lanes} from './ReactFiberLane.new';
1414

15-
import getComponentName from 'shared/getComponentName';
15+
import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFromFiber';
1616
import {Placement, ChildDeletion} from './ReactFiberFlags';
1717
import {
1818
getIteratorFn,
@@ -82,7 +82,7 @@ if (__DEV__) {
8282
);
8383
child._store.validated = true;
8484

85-
const componentName = getComponentName(returnFiber.type) || 'Component';
85+
const componentName = getComponentNameFromFiber(returnFiber) || 'Component';
8686

8787
if (ownerHasKeyUseWarning[componentName]) {
8888
return;
@@ -124,7 +124,8 @@ function coerceRef(
124124
element._owner.stateNode !== element._self
125125
)
126126
) {
127-
const componentName = getComponentName(returnFiber.type) || 'Component';
127+
const componentName =
128+
getComponentNameFromFiber(returnFiber) || 'Component';
128129
if (!didWarnAboutStringRefs[componentName]) {
129130
if (warnAboutStringRefs) {
130131
console.error(
@@ -232,7 +233,7 @@ function throwOnInvalidObjectType(returnFiber: Fiber, newChild: Object) {
232233

233234
function warnOnFunctionType(returnFiber: Fiber) {
234235
if (__DEV__) {
235-
const componentName = getComponentName(returnFiber.type) || 'Component';
236+
const componentName = getComponentNameFromFiber(returnFiber) || 'Component';
236237

237238
if (ownerHasFunctionTypeWarning[componentName]) {
238239
return;
@@ -1333,7 +1334,7 @@ function ChildReconciler(shouldTrackSideEffects) {
13331334
'%s(...): Nothing was returned from render. This usually means a ' +
13341335
'return statement is missing. Or, to render nothing, ' +
13351336
'return null.',
1336-
getComponentName(returnFiber.type) || 'Component',
1337+
getComponentNameFromFiber(returnFiber) || 'Component',
13371338
);
13381339
}
13391340
}

packages/react-reconciler/src/ReactChildFiber.old.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {ReactPortal} from 'shared/ReactTypes';
1212
import type {Fiber} from './ReactInternalTypes';
1313
import type {Lanes} from './ReactFiberLane.old';
1414

15-
import getComponentName from 'shared/getComponentName';
15+
import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFromFiber';
1616
import {Placement, ChildDeletion} from './ReactFiberFlags';
1717
import {
1818
getIteratorFn,
@@ -82,7 +82,7 @@ if (__DEV__) {
8282
);
8383
child._store.validated = true;
8484

85-
const componentName = getComponentName(returnFiber.type) || 'Component';
85+
const componentName = getComponentNameFromFiber(returnFiber) || 'Component';
8686

8787
if (ownerHasKeyUseWarning[componentName]) {
8888
return;
@@ -124,7 +124,8 @@ function coerceRef(
124124
element._owner.stateNode !== element._self
125125
)
126126
) {
127-
const componentName = getComponentName(returnFiber.type) || 'Component';
127+
const componentName =
128+
getComponentNameFromFiber(returnFiber) || 'Component';
128129
if (!didWarnAboutStringRefs[componentName]) {
129130
if (warnAboutStringRefs) {
130131
console.error(
@@ -232,7 +233,7 @@ function throwOnInvalidObjectType(returnFiber: Fiber, newChild: Object) {
232233

233234
function warnOnFunctionType(returnFiber: Fiber) {
234235
if (__DEV__) {
235-
const componentName = getComponentName(returnFiber.type) || 'Component';
236+
const componentName = getComponentNameFromFiber(returnFiber) || 'Component';
236237

237238
if (ownerHasFunctionTypeWarning[componentName]) {
238239
return;
@@ -1333,7 +1334,7 @@ function ChildReconciler(shouldTrackSideEffects) {
13331334
'%s(...): Nothing was returned from render. This usually means a ' +
13341335
'return statement is missing. Or, to render nothing, ' +
13351336
'return null.',
1336-
getComponentName(returnFiber.type) || 'Component',
1337+
getComponentNameFromFiber(returnFiber) || 'Component',
13371338
);
13381339
}
13391340
}

packages/react-reconciler/src/ReactCurrentFiber.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {Fiber} from './ReactInternalTypes';
1111

1212
import ReactSharedInternals from 'shared/ReactSharedInternals';
1313
import {getStackByFiberInDevAndProd} from './ReactFiberComponentStack';
14-
import getComponentName from 'shared/getComponentName';
14+
import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFromFiber';
1515

1616
const ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
1717

@@ -25,7 +25,7 @@ export function getCurrentFiberOwnerNameInDevOrNull(): string | null {
2525
}
2626
const owner = current._debugOwner;
2727
if (owner !== null && typeof owner !== 'undefined') {
28-
return getComponentName(owner.type);
28+
return getComponentNameFromFiber(owner);
2929
}
3030
}
3131
return null;

0 commit comments

Comments
 (0)