Skip to content

Commit

Permalink
Improve readability of isValidElementType (#19251)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
behnammodi authored Jul 8, 2020
1 parent 7b0ef42 commit 77e8722
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions packages/shared/isValidElementType.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,39 @@ import {
} from 'shared/ReactSymbols';

export default function isValidElementType(type: mixed) {
return (
typeof type === 'string' ||
typeof type === 'function' ||
// Note: its typeof might be other than 'symbol' or 'number' if it's a polyfill.
if (typeof type === 'string' || typeof type === 'function') {
return true;
}

// Note: typeof might be other than 'symbol' or 'number' (e.g. if it's a polyfill).
if (
type === REACT_FRAGMENT_TYPE ||
type === REACT_PROFILER_TYPE ||
type === REACT_DEBUG_TRACING_MODE_TYPE ||
type === REACT_STRICT_MODE_TYPE ||
type === REACT_SUSPENSE_TYPE ||
type === REACT_SUSPENSE_LIST_TYPE ||
type === REACT_LEGACY_HIDDEN_TYPE ||
(typeof type === 'object' &&
type !== null &&
(type.$$typeof === REACT_LAZY_TYPE ||
type.$$typeof === REACT_MEMO_TYPE ||
type.$$typeof === REACT_PROVIDER_TYPE ||
type.$$typeof === REACT_CONTEXT_TYPE ||
type.$$typeof === REACT_FORWARD_REF_TYPE ||
type.$$typeof === REACT_FUNDAMENTAL_TYPE ||
type.$$typeof === REACT_RESPONDER_TYPE ||
type.$$typeof === REACT_SCOPE_TYPE ||
type.$$typeof === REACT_BLOCK_TYPE ||
type[(0: any)] === REACT_SERVER_BLOCK_TYPE))
);
type === REACT_LEGACY_HIDDEN_TYPE
) {
return true;
}

if (typeof type === 'object' && type !== null) {
if (
type.$$typeof === REACT_LAZY_TYPE ||
type.$$typeof === REACT_MEMO_TYPE ||
type.$$typeof === REACT_PROVIDER_TYPE ||
type.$$typeof === REACT_CONTEXT_TYPE ||
type.$$typeof === REACT_FORWARD_REF_TYPE ||
type.$$typeof === REACT_FUNDAMENTAL_TYPE ||
type.$$typeof === REACT_RESPONDER_TYPE ||
type.$$typeof === REACT_SCOPE_TYPE ||
type.$$typeof === REACT_BLOCK_TYPE ||
type[(0: any)] === REACT_SERVER_BLOCK_TYPE
) {
return true;
}
}

return false;
}

0 comments on commit 77e8722

Please sign in to comment.