mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
15b11d23f9
* Allow arbitrary types to be wrapped in pure This creates an outer fiber that container the pure check and an inner fiber that represents which ever type of component. * Add optimized fast path for simple pure function components Special cased when there are no defaultProps and it's a simple function component instead of class. This doesn't require an extra fiber. We could make it so that this also works with custom comparer but that means we have to go through one extra indirection to get to it. Maybe it's worth it, donno.
97 lines
2.5 KiB
JavaScript
97 lines
2.5 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its 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 type {LazyComponent} from 'shared/ReactLazyComponent';
|
|
|
|
import warningWithoutStack from 'shared/warningWithoutStack';
|
|
import {
|
|
REACT_CONCURRENT_MODE_TYPE,
|
|
REACT_CONTEXT_TYPE,
|
|
REACT_FORWARD_REF_TYPE,
|
|
REACT_FRAGMENT_TYPE,
|
|
REACT_PORTAL_TYPE,
|
|
REACT_PURE_TYPE,
|
|
REACT_PROFILER_TYPE,
|
|
REACT_PROVIDER_TYPE,
|
|
REACT_STRICT_MODE_TYPE,
|
|
REACT_SUSPENSE_TYPE,
|
|
REACT_LAZY_TYPE,
|
|
} from 'shared/ReactSymbols';
|
|
import {refineResolvedLazyComponent} from 'shared/ReactLazyComponent';
|
|
|
|
function getWrappedName(
|
|
outerType: mixed,
|
|
innerType: any,
|
|
wrapperName: string,
|
|
): string {
|
|
const functionName = innerType.displayName || innerType.name || '';
|
|
return (
|
|
(outerType: any).displayName ||
|
|
(functionName !== '' ? `${wrapperName}(${functionName})` : wrapperName)
|
|
);
|
|
}
|
|
|
|
function getComponentName(type: mixed): string | null {
|
|
if (type == null) {
|
|
// Host root, text node or just invalid type.
|
|
return null;
|
|
}
|
|
if (__DEV__) {
|
|
if (typeof (type: any).tag === 'number') {
|
|
warningWithoutStack(
|
|
false,
|
|
'Received an unexpected object in getComponentName(). ' +
|
|
'This is likely a bug in React. Please file an issue.',
|
|
);
|
|
}
|
|
}
|
|
if (typeof type === 'function') {
|
|
return type.displayName || type.name || null;
|
|
}
|
|
if (typeof type === 'string') {
|
|
return type;
|
|
}
|
|
switch (type) {
|
|
case REACT_CONCURRENT_MODE_TYPE:
|
|
return 'ConcurrentMode';
|
|
case REACT_FRAGMENT_TYPE:
|
|
return 'Fragment';
|
|
case REACT_PORTAL_TYPE:
|
|
return 'Portal';
|
|
case REACT_PROFILER_TYPE:
|
|
return `Profiler`;
|
|
case REACT_STRICT_MODE_TYPE:
|
|
return 'StrictMode';
|
|
case REACT_SUSPENSE_TYPE:
|
|
return 'Suspense';
|
|
}
|
|
if (typeof type === 'object') {
|
|
switch (type.$$typeof) {
|
|
case REACT_CONTEXT_TYPE:
|
|
return 'Context.Consumer';
|
|
case REACT_PROVIDER_TYPE:
|
|
return 'Context.Provider';
|
|
case REACT_FORWARD_REF_TYPE:
|
|
return getWrappedName(type, type.render, 'ForwardRef');
|
|
case REACT_PURE_TYPE:
|
|
return getComponentName(type.type);
|
|
case REACT_LAZY_TYPE: {
|
|
const thenable: LazyComponent<mixed> = (type: any);
|
|
const resolvedThenable = refineResolvedLazyComponent(thenable);
|
|
if (resolvedThenable) {
|
|
return getComponentName(resolvedThenable);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export default getComponentName;
|