Skip to content

Commit b630219

Browse files
authored
[refactor] move isValidElementType to react-is (#32518)
1 parent 7943da1 commit b630219

File tree

3 files changed

+55
-87
lines changed

3 files changed

+55
-87
lines changed

packages/react-is/src/ReactIs.js

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,20 @@ import {
2424
REACT_SUSPENSE_TYPE,
2525
REACT_SUSPENSE_LIST_TYPE,
2626
REACT_VIEW_TRANSITION_TYPE,
27+
REACT_SCOPE_TYPE,
28+
REACT_LEGACY_HIDDEN_TYPE,
29+
REACT_TRACING_MARKER_TYPE,
2730
} from 'shared/ReactSymbols';
28-
import isValidElementType from 'shared/isValidElementType';
29-
import {enableRenderableContext} from 'shared/ReactFeatureFlags';
31+
32+
import {
33+
enableRenderableContext,
34+
enableScopeAPI,
35+
enableTransitionTracing,
36+
enableLegacyHidden,
37+
enableViewTransition,
38+
} from 'shared/ReactFeatureFlags';
39+
40+
const REACT_CLIENT_REFERENCE: symbol = Symbol.for('react.client.reference');
3041

3142
export function typeOf(object: any): mixed {
3243
if (typeof object === 'object' && object !== null) {
@@ -91,7 +102,47 @@ export const StrictMode = REACT_STRICT_MODE_TYPE;
91102
export const Suspense = REACT_SUSPENSE_TYPE;
92103
export const SuspenseList = REACT_SUSPENSE_LIST_TYPE;
93104

94-
export {isValidElementType};
105+
export function isValidElementType(type: mixed): boolean {
106+
if (typeof type === 'string' || typeof type === 'function') {
107+
return true;
108+
}
109+
110+
// Note: typeof might be other than 'symbol' or 'number' (e.g. if it's a polyfill).
111+
if (
112+
type === REACT_FRAGMENT_TYPE ||
113+
type === REACT_PROFILER_TYPE ||
114+
type === REACT_STRICT_MODE_TYPE ||
115+
type === REACT_SUSPENSE_TYPE ||
116+
type === REACT_SUSPENSE_LIST_TYPE ||
117+
(enableLegacyHidden && type === REACT_LEGACY_HIDDEN_TYPE) ||
118+
(enableScopeAPI && type === REACT_SCOPE_TYPE) ||
119+
(enableTransitionTracing && type === REACT_TRACING_MARKER_TYPE) ||
120+
(enableViewTransition && type === REACT_VIEW_TRANSITION_TYPE)
121+
) {
122+
return true;
123+
}
124+
125+
if (typeof type === 'object' && type !== null) {
126+
if (
127+
type.$$typeof === REACT_LAZY_TYPE ||
128+
type.$$typeof === REACT_MEMO_TYPE ||
129+
type.$$typeof === REACT_CONTEXT_TYPE ||
130+
(!enableRenderableContext && type.$$typeof === REACT_PROVIDER_TYPE) ||
131+
(enableRenderableContext && type.$$typeof === REACT_CONSUMER_TYPE) ||
132+
type.$$typeof === REACT_FORWARD_REF_TYPE ||
133+
// This needs to include all possible module reference object
134+
// types supported by any Flight configuration anywhere since
135+
// we don't know which Flight build this will end up being used
136+
// with.
137+
type.$$typeof === REACT_CLIENT_REFERENCE ||
138+
type.getModuleId !== undefined
139+
) {
140+
return true;
141+
}
142+
}
143+
144+
return false;
145+
}
95146

96147
export function isContextConsumer(object: any): boolean {
97148
if (enableRenderableContext) {

packages/react/src/ReactMemo.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99

1010
import {REACT_MEMO_TYPE} from 'shared/ReactSymbols';
1111

12-
import isValidElementType from 'shared/isValidElementType';
13-
1412
export function memo<Props>(
1513
type: React$ElementType,
1614
compare?: (oldProps: Props, newProps: Props) => boolean,
1715
) {
1816
if (__DEV__) {
19-
if (!isValidElementType(type)) {
17+
if (type == null) {
2018
console.error(
2119
'memo: The first argument must be a component. Instead ' +
2220
'received: %s',

packages/shared/isValidElementType.js

Lines changed: 0 additions & 81 deletions
This file was deleted.

0 commit comments

Comments
 (0)