Skip to content

Commit dbaeccd

Browse files
committed
[refactor] move isValidElementType to react-is
1 parent 7943da1 commit dbaeccd

File tree

3 files changed

+57
-87
lines changed

3 files changed

+57
-87
lines changed

packages/react-is/src/ReactIs.js

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,21 @@ 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_OFFSCREEN_TYPE,
30+
REACT_TRACING_MARKER_TYPE,
2731
} from 'shared/ReactSymbols';
28-
import isValidElementType from 'shared/isValidElementType';
29-
import {enableRenderableContext} from 'shared/ReactFeatureFlags';
32+
33+
import {
34+
enableRenderableContext,
35+
enableScopeAPI,
36+
enableTransitionTracing,
37+
enableLegacyHidden,
38+
enableViewTransition,
39+
} from 'shared/ReactFeatureFlags';
40+
41+
const REACT_CLIENT_REFERENCE: symbol = Symbol.for('react.client.reference');
3042

3143
export function typeOf(object: any): mixed {
3244
if (typeof object === 'object' && object !== null) {
@@ -91,7 +103,48 @@ export const StrictMode = REACT_STRICT_MODE_TYPE;
91103
export const Suspense = REACT_SUSPENSE_TYPE;
92104
export const SuspenseList = REACT_SUSPENSE_LIST_TYPE;
93105

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

96149
export function isContextConsumer(object: any): boolean {
97150
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)