[refactor] move isValidElementType to react-is (#32518)

This commit is contained in:
Ricky 2025-03-20 16:51:33 -04:00 committed by GitHub
parent 7943da1e81
commit b630219b13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 55 additions and 87 deletions

View File

@ -24,9 +24,20 @@ import {
REACT_SUSPENSE_TYPE,
REACT_SUSPENSE_LIST_TYPE,
REACT_VIEW_TRANSITION_TYPE,
REACT_SCOPE_TYPE,
REACT_LEGACY_HIDDEN_TYPE,
REACT_TRACING_MARKER_TYPE,
} from 'shared/ReactSymbols';
import isValidElementType from 'shared/isValidElementType';
import {enableRenderableContext} from 'shared/ReactFeatureFlags';
import {
enableRenderableContext,
enableScopeAPI,
enableTransitionTracing,
enableLegacyHidden,
enableViewTransition,
} from 'shared/ReactFeatureFlags';
const REACT_CLIENT_REFERENCE: symbol = Symbol.for('react.client.reference');
export function typeOf(object: any): mixed {
if (typeof object === 'object' && object !== null) {
@ -91,7 +102,47 @@ export const StrictMode = REACT_STRICT_MODE_TYPE;
export const Suspense = REACT_SUSPENSE_TYPE;
export const SuspenseList = REACT_SUSPENSE_LIST_TYPE;
export {isValidElementType};
export function isValidElementType(type: mixed): boolean {
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_STRICT_MODE_TYPE ||
type === REACT_SUSPENSE_TYPE ||
type === REACT_SUSPENSE_LIST_TYPE ||
(enableLegacyHidden && type === REACT_LEGACY_HIDDEN_TYPE) ||
(enableScopeAPI && type === REACT_SCOPE_TYPE) ||
(enableTransitionTracing && type === REACT_TRACING_MARKER_TYPE) ||
(enableViewTransition && type === REACT_VIEW_TRANSITION_TYPE)
) {
return true;
}
if (typeof type === 'object' && type !== null) {
if (
type.$$typeof === REACT_LAZY_TYPE ||
type.$$typeof === REACT_MEMO_TYPE ||
type.$$typeof === REACT_CONTEXT_TYPE ||
(!enableRenderableContext && type.$$typeof === REACT_PROVIDER_TYPE) ||
(enableRenderableContext && type.$$typeof === REACT_CONSUMER_TYPE) ||
type.$$typeof === REACT_FORWARD_REF_TYPE ||
// This needs to include all possible module reference object
// types supported by any Flight configuration anywhere since
// we don't know which Flight build this will end up being used
// with.
type.$$typeof === REACT_CLIENT_REFERENCE ||
type.getModuleId !== undefined
) {
return true;
}
}
return false;
}
export function isContextConsumer(object: any): boolean {
if (enableRenderableContext) {

View File

@ -9,14 +9,12 @@
import {REACT_MEMO_TYPE} from 'shared/ReactSymbols';
import isValidElementType from 'shared/isValidElementType';
export function memo<Props>(
type: React$ElementType,
compare?: (oldProps: Props, newProps: Props) => boolean,
) {
if (__DEV__) {
if (!isValidElementType(type)) {
if (type == null) {
console.error(
'memo: The first argument must be a component. Instead ' +
'received: %s',

View File

@ -1,81 +0,0 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import {
REACT_CONTEXT_TYPE,
REACT_CONSUMER_TYPE,
REACT_PROVIDER_TYPE,
REACT_FORWARD_REF_TYPE,
REACT_FRAGMENT_TYPE,
REACT_PROFILER_TYPE,
REACT_STRICT_MODE_TYPE,
REACT_SUSPENSE_TYPE,
REACT_SUSPENSE_LIST_TYPE,
REACT_MEMO_TYPE,
REACT_LAZY_TYPE,
REACT_SCOPE_TYPE,
REACT_LEGACY_HIDDEN_TYPE,
REACT_TRACING_MARKER_TYPE,
REACT_VIEW_TRANSITION_TYPE,
REACT_ACTIVITY_TYPE,
} from 'shared/ReactSymbols';
import {
enableScopeAPI,
enableTransitionTracing,
enableLegacyHidden,
enableRenderableContext,
enableViewTransition,
} from './ReactFeatureFlags';
const REACT_CLIENT_REFERENCE: symbol = Symbol.for('react.client.reference');
// This function is deprecated. Don't use. Only the renderer knows what a valid type is.
// TODO: Delete this now that owner stacks shipped.
export default function isValidElementType(type: mixed): boolean {
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_STRICT_MODE_TYPE ||
type === REACT_SUSPENSE_TYPE ||
type === REACT_SUSPENSE_LIST_TYPE ||
(enableLegacyHidden && type === REACT_LEGACY_HIDDEN_TYPE) ||
type === REACT_ACTIVITY_TYPE ||
(enableScopeAPI && type === REACT_SCOPE_TYPE) ||
(enableTransitionTracing && type === REACT_TRACING_MARKER_TYPE) ||
(enableViewTransition && type === REACT_VIEW_TRANSITION_TYPE)
) {
return true;
}
if (typeof type === 'object' && type !== null) {
if (
type.$$typeof === REACT_LAZY_TYPE ||
type.$$typeof === REACT_MEMO_TYPE ||
type.$$typeof === REACT_CONTEXT_TYPE ||
(!enableRenderableContext && type.$$typeof === REACT_PROVIDER_TYPE) ||
(enableRenderableContext && type.$$typeof === REACT_CONSUMER_TYPE) ||
type.$$typeof === REACT_FORWARD_REF_TYPE ||
// This needs to include all possible module reference object
// types supported by any Flight configuration anywhere since
// we don't know which Flight build this will end up being used
// with.
type.$$typeof === REACT_CLIENT_REFERENCE ||
type.getModuleId !== undefined
) {
return true;
}
}
return false;
}