Skip to content

Commit 77e8722

Browse files
authored
Improve readability of isValidElementType (facebook#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
1 parent 7b0ef42 commit 77e8722

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

packages/shared/isValidElementType.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,39 @@ import {
2828
} from 'shared/ReactSymbols';
2929

3030
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 (
3537
type === REACT_FRAGMENT_TYPE ||
3638
type === REACT_PROFILER_TYPE ||
3739
type === REACT_DEBUG_TRACING_MODE_TYPE ||
3840
type === REACT_STRICT_MODE_TYPE ||
3941
type === REACT_SUSPENSE_TYPE ||
4042
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;
5566
}

0 commit comments

Comments
 (0)