forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetComponentName.js
59 lines (55 loc) · 1.49 KB
/
getComponentName.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {Fiber} from 'react-reconciler/src/ReactFiber';
import {
REACT_ASYNC_MODE_TYPE,
REACT_CONTEXT_TYPE,
REACT_FORWARD_REF_TYPE,
REACT_FRAGMENT_TYPE,
REACT_PORTAL_TYPE,
REACT_PROFILER_TYPE,
REACT_PROVIDER_TYPE,
REACT_STRICT_MODE_TYPE,
} from 'shared/ReactSymbols';
function getComponentName(fiber: Fiber): string | null {
const {type} = fiber;
if (typeof type === 'function') {
return type.displayName || type.name;
}
if (typeof type === 'string') {
return type;
}
switch (type) {
case REACT_ASYNC_MODE_TYPE:
return 'AsyncMode';
case REACT_CONTEXT_TYPE:
return 'Context.Consumer';
case REACT_FRAGMENT_TYPE:
return 'ReactFragment';
case REACT_PORTAL_TYPE:
return 'ReactPortal';
case REACT_PROFILER_TYPE:
return `Profiler(${fiber.pendingProps.id})`;
case REACT_PROVIDER_TYPE:
return 'Context.Provider';
case REACT_STRICT_MODE_TYPE:
return 'StrictMode';
}
if (typeof type === 'object' && type !== null) {
switch (type.$$typeof) {
case REACT_FORWARD_REF_TYPE:
const functionName = type.render.displayName || type.render.name || '';
return functionName !== ''
? `ForwardRef(${functionName})`
: 'ForwardRef';
}
}
return null;
}
export default getComponentName;