Improve readability of isValidElementType (#19251)
* improve readability * replace condition by switch/case * replace condition by switch/case * remove unnecessary braces * replace switch/case to ifs * replace switch/case to ifs * fix by multiline if statements * fix multiple if statements
Behnam Mohammadi committed
Jul 8, 2020 at 22:55 UTC
77e872217c7f4044ae4f96dd99bf999efb601040
1 file changed
+29
-18
packages/shared/isValidElementType.js
+29
-18
@@ -28,28 +28,39 @@ import {
28
} from 'shared/ReactSymbols';
29
30
export default function isValidElementType(type: mixed) {
31
- return (
32
- typeof type === 'string' ||
33
- typeof type === 'function' ||
34
- // Note: its typeof might be other than 'symbol' or 'number' if it's a polyfill.
31
+ if (typeof type === 'string' || typeof type === 'function') {
32
+ return true;
33
+ }
34
+
35
+ // Note: typeof might be other than 'symbol' or 'number' (e.g. if it's a polyfill).
36
+ if (
37
type === REACT_FRAGMENT_TYPE ||
38
type === REACT_PROFILER_TYPE ||
39
type === REACT_DEBUG_TRACING_MODE_TYPE ||
40
type === REACT_STRICT_MODE_TYPE ||
41
type === REACT_SUSPENSE_TYPE ||
42
type === REACT_SUSPENSE_LIST_TYPE ||
41
- type === REACT_LEGACY_HIDDEN_TYPE ||
42
- (typeof type === 'object' &&
43
- type !== null &&
44
- (type.$$typeof === REACT_LAZY_TYPE ||
45
- type.$$typeof === REACT_MEMO_TYPE ||
46
- type.$$typeof === REACT_PROVIDER_TYPE ||
47
- type.$$typeof === REACT_CONTEXT_TYPE ||
48
- type.$$typeof === REACT_FORWARD_REF_TYPE ||
49
- type.$$typeof === REACT_FUNDAMENTAL_TYPE ||
50
- type.$$typeof === REACT_RESPONDER_TYPE ||
51
- type.$$typeof === REACT_SCOPE_TYPE ||
52
- type.$$typeof === REACT_BLOCK_TYPE ||
53
- type[(0: any)] === REACT_SERVER_BLOCK_TYPE))
54
- );
43
+ type === REACT_LEGACY_HIDDEN_TYPE
44
+ ) {
45
+ return true;
46
+ }
47
+
48
+ if (typeof type === 'object' && type !== null) {
49
+ if (
50
+ type.$$typeof === REACT_LAZY_TYPE ||
51
+ type.$$typeof === REACT_MEMO_TYPE ||
52
+ type.$$typeof === REACT_PROVIDER_TYPE ||
53
+ type.$$typeof === REACT_CONTEXT_TYPE ||
54
+ type.$$typeof === REACT_FORWARD_REF_TYPE ||
55
+ type.$$typeof === REACT_FUNDAMENTAL_TYPE ||
56
+ type.$$typeof === REACT_RESPONDER_TYPE ||
57
+ type.$$typeof === REACT_SCOPE_TYPE ||
58
+ type.$$typeof === REACT_BLOCK_TYPE ||
59
+ type[(0: any)] === REACT_SERVER_BLOCK_TYPE
60
+ ) {
61
+ return true;
62
+ }
63
+ }
64
+
65
+ return false;
66
}