Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] move isValidElementType to react-is #32518

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 56 additions & 3 deletions packages/react-is/src/ReactIs.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,21 @@ import {
REACT_SUSPENSE_TYPE,
REACT_SUSPENSE_LIST_TYPE,
REACT_VIEW_TRANSITION_TYPE,
REACT_SCOPE_TYPE,
REACT_LEGACY_HIDDEN_TYPE,
REACT_OFFSCREEN_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) {
Expand Down Expand Up @@ -91,7 +103,48 @@ 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) ||
type === REACT_OFFSCREEN_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) {
Expand Down
4 changes: 1 addition & 3 deletions packages/react/src/ReactMemo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
81 changes: 0 additions & 81 deletions packages/shared/isValidElementType.js

This file was deleted.

Loading