React 深度学习:isValidElementType

2,567 阅读1分钟

源码

import {
  REACT_CONCURRENT_MODE_TYPE,
  REACT_CONTEXT_TYPE,
  REACT_FORWARD_REF_TYPE,
  REACT_FRAGMENT_TYPE,
  REACT_PROFILER_TYPE,
  REACT_PROVIDER_TYPE,
  REACT_STRICT_MODE_TYPE,
  REACT_SUSPENSE_TYPE,
  REACT_MEMO_TYPE,
  REACT_LAZY_TYPE,
} from 'shared/ReactSymbols';

export default function isValidElementType(type: mixed) {
  return (
    typeof type === 'string' ||
    typeof type === 'function' ||
    // Note: its typeof might be other than 'symbol' or 'number' if it's a polyfill.
    type === REACT_FRAGMENT_TYPE ||
    type === REACT_CONCURRENT_MODE_TYPE ||
    type === REACT_PROFILER_TYPE ||
    type === REACT_STRICT_MODE_TYPE ||
    type === REACT_SUSPENSE_TYPE ||
    (typeof type === 'object' &&
      type !== null &&
      (type.?typeof === REACT_LAZY_TYPE ||
        type.?typeof === REACT_MEMO_TYPE ||
        type.?typeof === REACT_PROVIDER_TYPE ||
        type.?typeof === REACT_CONTEXT_TYPE ||
        type.?typeof === REACT_FORWARD_REF_TYPE))
  );
}

isValidElementType 方法用于判断目标是不是一个有效的 React 元素类型,以下类型会被认为是有效的: