[refactor] move isValidElementType to react-is (#32518)
Ricky committed
Mar 20, 2025 at 16:51 UTC
b630219b1377f3117036b1c6118676c16fdb21b7
3 files changed
+55
-87
packages/react-is/src/ReactIs.js
+54
-3
@@ -24,9 +24,20 @@ import {
24
REACT_SUSPENSE_TYPE,
25
REACT_SUSPENSE_LIST_TYPE,
26
REACT_VIEW_TRANSITION_TYPE,
27
+ REACT_SCOPE_TYPE,
28
+ REACT_LEGACY_HIDDEN_TYPE,
29
+ REACT_TRACING_MARKER_TYPE,
30
} 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');
41
42
export function typeOf(object: any): mixed {
43
if (typeof object === 'object' && object !== null) {
@@ -91,7 +102,47 @@ export const StrictMode = REACT_STRICT_MODE_TYPE;
102
export const Suspense = REACT_SUSPENSE_TYPE;
103
export const SuspenseList = REACT_SUSPENSE_LIST_TYPE;
104
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
+}
146
147
export function isContextConsumer(object: any): boolean {
148
if (enableRenderableContext) {
packages/react/src/ReactMemo.js
+1
-3
@@ -9,14 +9,12 @@
9
10
import {REACT_MEMO_TYPE} from 'shared/ReactSymbols';
11
12
-import isValidElementType from 'shared/isValidElementType';
13
-
12
export function memo<Props>(
13
type: React$ElementType,
14
compare?: (oldProps: Props, newProps: Props) => boolean,
15
) {
16
if (__DEV__) {
19
- if (!isValidElementType(type)) {
17
+ if (type == null) {
18
console.error(
19
'memo: The first argument must be a component. Instead ' +
20
'received: %s',
packages/shared/isValidElementType.js
deleted
-81
@@ -1,81 +0,0 @@
1
-/**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- * @flow
8
- */
9
-
10
-import {
11
- REACT_CONTEXT_TYPE,
12
- REACT_CONSUMER_TYPE,
13
- REACT_PROVIDER_TYPE,
14
- REACT_FORWARD_REF_TYPE,
15
- REACT_FRAGMENT_TYPE,
16
- REACT_PROFILER_TYPE,
17
- REACT_STRICT_MODE_TYPE,
18
- REACT_SUSPENSE_TYPE,
19
- REACT_SUSPENSE_LIST_TYPE,
20
- REACT_MEMO_TYPE,
21
- REACT_LAZY_TYPE,
22
- REACT_SCOPE_TYPE,
23
- REACT_LEGACY_HIDDEN_TYPE,
24
- REACT_TRACING_MARKER_TYPE,
25
- REACT_VIEW_TRANSITION_TYPE,
26
- REACT_ACTIVITY_TYPE,
27
-} from 'shared/ReactSymbols';
28
-import {
29
- enableScopeAPI,
30
- enableTransitionTracing,
31
- enableLegacyHidden,
32
- enableRenderableContext,
33
- enableViewTransition,
34
-} from './ReactFeatureFlags';
35
-
36
-const REACT_CLIENT_REFERENCE: symbol = Symbol.for('react.client.reference');
37
-
38
-// This function is deprecated. Don't use. Only the renderer knows what a valid type is.
39
-// TODO: Delete this now that owner stacks shipped.
40
-export default function isValidElementType(type: mixed): boolean {
41
- if (typeof type === 'string' || typeof type === 'function') {
42
- return true;
43
- }
44
-
45
- // Note: typeof might be other than 'symbol' or 'number' (e.g. if it's a polyfill).
46
- if (
47
- type === REACT_FRAGMENT_TYPE ||
48
- type === REACT_PROFILER_TYPE ||
49
- type === REACT_STRICT_MODE_TYPE ||
50
- type === REACT_SUSPENSE_TYPE ||
51
- type === REACT_SUSPENSE_LIST_TYPE ||
52
- (enableLegacyHidden && type === REACT_LEGACY_HIDDEN_TYPE) ||
53
- type === REACT_ACTIVITY_TYPE ||
54
- (enableScopeAPI && type === REACT_SCOPE_TYPE) ||
55
- (enableTransitionTracing && type === REACT_TRACING_MARKER_TYPE) ||
56
- (enableViewTransition && type === REACT_VIEW_TRANSITION_TYPE)
57
- ) {
58
- return true;
59
- }
60
-
61
- if (typeof type === 'object' && type !== null) {
62
- if (
63
- type.$$typeof === REACT_LAZY_TYPE ||
64
- type.$$typeof === REACT_MEMO_TYPE ||
65
- type.$$typeof === REACT_CONTEXT_TYPE ||
66
- (!enableRenderableContext && type.$$typeof === REACT_PROVIDER_TYPE) ||
67
- (enableRenderableContext && type.$$typeof === REACT_CONSUMER_TYPE) ||
68
- type.$$typeof === REACT_FORWARD_REF_TYPE ||
69
- // This needs to include all possible module reference object
70
- // types supported by any Flight configuration anywhere since
71
- // we don't know which Flight build this will end up being used
72
- // with.
73
- type.$$typeof === REACT_CLIENT_REFERENCE ||
74
- type.getModuleId !== undefined
75
- ) {
76
- return true;
77
- }
78
- }
79
-
80
- return false;
81
-}