MCPcopy
hub / github.com/facebook/react / getComponentNameFromType

Function getComponentNameFromType

packages/shared/getComponentNameFromType.js:57–134  ·  view source on GitHub ↗
(type: mixed)

Source from the content-addressed store, hash-verified

55
56// Note that the reconciler package should generally prefer to use getComponentNameFromFiber() instead.
57export default function getComponentNameFromType(type: mixed): string | null {
58 if (type == null) {
59 // Host root, text node or just invalid type.
60 return null;
61 }
62 if (typeof type === 'function') {
63 if ((type: any).$$typeof === REACT_CLIENT_REFERENCE) {
64 // TODO: Create a convention for naming client references with debug info.
65 return null;
66 }
67 return (type: any).displayName || type.name || null;
68 }
69 if (typeof type === 'string') {
70 return type;
71 }
72 switch (type) {
73 case REACT_FRAGMENT_TYPE:
74 return 'Fragment';
75 case REACT_PROFILER_TYPE:
76 return 'Profiler';
77 case REACT_STRICT_MODE_TYPE:
78 return 'StrictMode';
79 case REACT_SUSPENSE_TYPE:
80 return 'Suspense';
81 case REACT_SUSPENSE_LIST_TYPE:
82 return 'SuspenseList';
83 case REACT_ACTIVITY_TYPE:
84 return 'Activity';
85 case REACT_VIEW_TRANSITION_TYPE:
86 if (enableViewTransition) {
87 return 'ViewTransition';
88 }
89 // Fall through
90 case REACT_TRACING_MARKER_TYPE:
91 if (enableTransitionTracing) {
92 return 'TracingMarker';
93 }
94 }
95 if (typeof type === 'object') {
96 if (__DEV__) {
97 if (typeof (type: any).tag === 'number') {
98 console.error(
99 'Received an unexpected object in getComponentNameFromType(). ' +
100 'This is likely a bug in React. Please file an issue.',
101 );
102 }
103 }
104 switch (type.$$typeof) {
105 case REACT_PORTAL_TYPE:
106 return 'Portal';
107 case REACT_CONTEXT_TYPE:
108 const context: ReactContext<any> = (type: any);
109 return getContextName(context);
110 case REACT_CONSUMER_TYPE:
111 const consumer: ReactConsumerType<any> = (type: any);
112 return getContextName(consumer._context) + '.Consumer';
113 case REACT_FORWARD_REF_TYPE:
114 return getWrappedName(type, type.render, 'ForwardRef');

Callers 15

getTaskNameFunction · 0.85
jsxDEVImplFunction · 0.85
ifFunction · 0.85
rejectFunction · 0.85
findNodeHandleFunction · 0.85
createHierarchyFunction · 0.85
addValueToPropertiesFunction · 0.85
findDOMNodeFunction · 0.85

Calls 4

getContextNameFunction · 0.70
getWrappedNameFunction · 0.70
errorMethod · 0.65
initFunction · 0.50

Tested by

no test coverage detected