2016-10-30 07:43:34 +08:00
|
|
|
/**
|
2018-09-08 06:11:23 +08:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-10-30 07:43:34 +08:00
|
|
|
*
|
2017-09-25 04:48:13 +08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-10-30 07:43:34 +08:00
|
|
|
*
|
2017-04-05 23:47:29 +08:00
|
|
|
* @flow
|
2016-10-30 07:43:34 +08:00
|
|
|
*/
|
|
|
|
|
2020-03-05 12:52:48 +08:00
|
|
|
import type {LazyComponent} from 'react/src/ReactLazy';
|
2021-03-06 05:02:02 +08:00
|
|
|
import type {ReactContext, ReactProviderType} from 'shared/ReactTypes';
|
Accept promise as element type (#13397)
* Accept promise as element type
On the initial render, the element will suspend as if a promise were
thrown from inside the body of the unresolved component. Siblings should
continue rendering and if the parent is a Placeholder, the promise
should be captured by that Placeholder.
When the promise resolves, rendering resumes. If the resolved value
has a `default` property, it is assumed to be the default export of
an ES module, and we use that as the component type. If it does not have
a `default` property, we use the resolved value itself.
The resolved value is stored as an expando on the promise/thenable.
* Use special types of work for lazy components
Because reconciliation is a hot path, this adds ClassComponentLazy,
FunctionalComponentLazy, and ForwardRefLazy as special types of work.
The other types are not supported, but wouldn't be placed into a
separate module regardless.
* Resolve defaultProps for lazy types
* Remove some calls to isContextProvider
isContextProvider checks the fiber tag, but it's typically called after
we've already refined the type of work. We should get rid of it. I
removed some of them in the previous commit, and deleted a few more
in this one. I left a few behind because the remaining ones would
require additional refactoring that feels outside the scope of this PR.
* Remove getLazyComponentTypeIfResolved
* Return baseProps instead of null
The caller compares the result to baseProps to see if anything changed.
* Avoid redundant checks by inlining getFiberTagFromObjectType
* Move tag resolution to ReactFiber module
* Pass next props to update* functions
We should do this with all types of work in the future.
* Refine component type before pushing/popping context
Removes unnecessary checks.
* Replace all occurrences of _reactResult with helper
* Move shared thenable logic to `shared` package
* Check type of wrapper object before resolving to `default` export
* Return resolved tag instead of reassigning
2018-08-17 00:21:59 +08:00
|
|
|
|
2017-12-13 07:04:40 +08:00
|
|
|
import {
|
2018-05-15 01:10:36 +08:00
|
|
|
REACT_CONTEXT_TYPE,
|
2018-05-11 06:25:32 +08:00
|
|
|
REACT_FORWARD_REF_TYPE,
|
2017-12-13 07:04:40 +08:00
|
|
|
REACT_FRAGMENT_TYPE,
|
|
|
|
REACT_PORTAL_TYPE,
|
2018-10-21 00:46:23 +08:00
|
|
|
REACT_MEMO_TYPE,
|
2018-05-11 06:25:32 +08:00
|
|
|
REACT_PROFILER_TYPE,
|
2018-05-15 01:10:36 +08:00
|
|
|
REACT_PROVIDER_TYPE,
|
2018-05-11 06:25:32 +08:00
|
|
|
REACT_STRICT_MODE_TYPE,
|
2018-10-11 00:02:04 +08:00
|
|
|
REACT_SUSPENSE_TYPE,
|
2019-06-20 10:34:28 +08:00
|
|
|
REACT_SUSPENSE_LIST_TYPE,
|
2018-10-19 10:57:12 +08:00
|
|
|
REACT_LAZY_TYPE,
|
2020-12-19 02:57:24 +08:00
|
|
|
REACT_CACHE_TYPE,
|
2022-02-12 00:42:55 +08:00
|
|
|
REACT_TRACING_MARKER_TYPE,
|
2022-03-08 20:55:32 +08:00
|
|
|
REACT_SERVER_CONTEXT_TYPE,
|
2017-12-13 07:04:40 +08:00
|
|
|
} from 'shared/ReactSymbols';
|
|
|
|
|
2022-03-08 20:55:32 +08:00
|
|
|
import {
|
|
|
|
enableServerContext,
|
|
|
|
enableTransitionTracing,
|
|
|
|
enableCache,
|
|
|
|
} from './ReactFeatureFlags';
|
2022-03-08 13:13:22 +08:00
|
|
|
|
2021-03-06 05:02:02 +08:00
|
|
|
// Keep in sync with react-reconciler/getComponentNameFromFiber
|
2018-10-12 10:15:14 +08:00
|
|
|
function getWrappedName(
|
|
|
|
outerType: mixed,
|
|
|
|
innerType: any,
|
|
|
|
wrapperName: string,
|
|
|
|
): string {
|
2021-05-04 23:40:16 +08:00
|
|
|
const displayName = (outerType: any).displayName;
|
|
|
|
if (displayName) {
|
|
|
|
return displayName;
|
|
|
|
}
|
2018-10-12 10:15:14 +08:00
|
|
|
const functionName = innerType.displayName || innerType.name || '';
|
2021-05-04 23:40:16 +08:00
|
|
|
return functionName !== '' ? `${wrapperName}(${functionName})` : wrapperName;
|
2018-10-12 10:15:14 +08:00
|
|
|
}
|
|
|
|
|
2021-03-06 05:02:02 +08:00
|
|
|
// Keep in sync with react-reconciler/getComponentNameFromFiber
|
2020-03-06 02:13:52 +08:00
|
|
|
function getContextName(type: ReactContext<any>) {
|
|
|
|
return type.displayName || 'Context';
|
|
|
|
}
|
|
|
|
|
2021-03-06 05:02:02 +08:00
|
|
|
// Note that the reconciler package should generally prefer to use getComponentNameFromFiber() instead.
|
|
|
|
export default function getComponentNameFromType(type: mixed): string | null {
|
2018-07-12 22:32:06 +08:00
|
|
|
if (type == null) {
|
|
|
|
// Host root, text node or just invalid type.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (__DEV__) {
|
|
|
|
if (typeof (type: any).tag === 'number') {
|
2019-12-15 02:09:25 +08:00
|
|
|
console.error(
|
2021-05-07 09:03:21 +08:00
|
|
|
'Received an unexpected object in getComponentNameFromType(). ' +
|
2018-07-12 22:32:06 +08:00
|
|
|
'This is likely a bug in React. Please file an issue.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-12-13 07:04:40 +08:00
|
|
|
if (typeof type === 'function') {
|
2020-01-25 02:52:38 +08:00
|
|
|
return (type: any).displayName || type.name || null;
|
2017-12-13 07:04:40 +08:00
|
|
|
}
|
2017-10-02 20:43:45 +08:00
|
|
|
if (typeof type === 'string') {
|
|
|
|
return type;
|
2017-01-24 09:11:10 +08:00
|
|
|
}
|
2017-12-13 07:04:40 +08:00
|
|
|
switch (type) {
|
|
|
|
case REACT_FRAGMENT_TYPE:
|
2018-07-12 22:32:06 +08:00
|
|
|
return 'Fragment';
|
2017-12-13 07:04:40 +08:00
|
|
|
case REACT_PORTAL_TYPE:
|
2018-07-12 22:32:06 +08:00
|
|
|
return 'Portal';
|
2018-05-11 06:25:32 +08:00
|
|
|
case REACT_PROFILER_TYPE:
|
2020-09-21 20:35:21 +08:00
|
|
|
return 'Profiler';
|
2018-05-11 06:25:32 +08:00
|
|
|
case REACT_STRICT_MODE_TYPE:
|
|
|
|
return 'StrictMode';
|
2018-10-11 00:02:04 +08:00
|
|
|
case REACT_SUSPENSE_TYPE:
|
|
|
|
return 'Suspense';
|
2019-06-20 10:34:28 +08:00
|
|
|
case REACT_SUSPENSE_LIST_TYPE:
|
|
|
|
return 'SuspenseList';
|
2020-12-19 02:57:24 +08:00
|
|
|
case REACT_CACHE_TYPE:
|
2022-03-08 13:13:22 +08:00
|
|
|
if (enableCache) {
|
|
|
|
return 'Cache';
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line no-fallthrough
|
2022-02-12 00:42:55 +08:00
|
|
|
case REACT_TRACING_MARKER_TYPE:
|
2022-03-08 13:13:22 +08:00
|
|
|
if (enableTransitionTracing) {
|
|
|
|
return 'TracingMarker';
|
|
|
|
}
|
2016-10-30 07:43:34 +08:00
|
|
|
}
|
2018-07-12 22:32:06 +08:00
|
|
|
if (typeof type === 'object') {
|
2018-04-13 00:39:05 +08:00
|
|
|
switch (type.$$typeof) {
|
2018-07-12 22:32:06 +08:00
|
|
|
case REACT_CONTEXT_TYPE:
|
2020-03-06 02:13:52 +08:00
|
|
|
const context: ReactContext<any> = (type: any);
|
|
|
|
return getContextName(context) + '.Consumer';
|
2018-07-12 22:32:06 +08:00
|
|
|
case REACT_PROVIDER_TYPE:
|
2020-03-06 02:13:52 +08:00
|
|
|
const provider: ReactProviderType<any> = (type: any);
|
|
|
|
return getContextName(provider._context) + '.Provider';
|
2018-04-13 00:39:05 +08:00
|
|
|
case REACT_FORWARD_REF_TYPE:
|
2018-10-12 10:15:14 +08:00
|
|
|
return getWrappedName(type, type.render, 'ForwardRef');
|
2018-10-21 00:46:23 +08:00
|
|
|
case REACT_MEMO_TYPE:
|
2021-05-04 23:40:16 +08:00
|
|
|
const outerName = (type: any).displayName || null;
|
|
|
|
if (outerName !== null) {
|
|
|
|
return outerName;
|
|
|
|
}
|
|
|
|
return getComponentNameFromType(type.type) || 'Memo';
|
2018-10-19 10:57:12 +08:00
|
|
|
case REACT_LAZY_TYPE: {
|
2020-03-23 12:53:05 +08:00
|
|
|
const lazyComponent: LazyComponent<any, any> = (type: any);
|
2020-04-02 03:35:52 +08:00
|
|
|
const payload = lazyComponent._payload;
|
|
|
|
const init = lazyComponent._init;
|
2020-03-23 12:53:05 +08:00
|
|
|
try {
|
2021-03-06 05:02:02 +08:00
|
|
|
return getComponentNameFromType(init(payload));
|
2020-03-23 12:53:05 +08:00
|
|
|
} catch (x) {
|
|
|
|
return null;
|
2018-10-19 10:57:12 +08:00
|
|
|
}
|
2019-04-03 19:24:25 +08:00
|
|
|
}
|
2022-03-08 20:55:32 +08:00
|
|
|
case REACT_SERVER_CONTEXT_TYPE:
|
|
|
|
if (enableServerContext) {
|
|
|
|
const context2 = ((type: any): ReactContext<any>);
|
|
|
|
return (context2.displayName || context2._globalName) + '.Provider';
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line no-fallthrough
|
Accept promise as element type (#13397)
* Accept promise as element type
On the initial render, the element will suspend as if a promise were
thrown from inside the body of the unresolved component. Siblings should
continue rendering and if the parent is a Placeholder, the promise
should be captured by that Placeholder.
When the promise resolves, rendering resumes. If the resolved value
has a `default` property, it is assumed to be the default export of
an ES module, and we use that as the component type. If it does not have
a `default` property, we use the resolved value itself.
The resolved value is stored as an expando on the promise/thenable.
* Use special types of work for lazy components
Because reconciliation is a hot path, this adds ClassComponentLazy,
FunctionalComponentLazy, and ForwardRefLazy as special types of work.
The other types are not supported, but wouldn't be placed into a
separate module regardless.
* Resolve defaultProps for lazy types
* Remove some calls to isContextProvider
isContextProvider checks the fiber tag, but it's typically called after
we've already refined the type of work. We should get rid of it. I
removed some of them in the previous commit, and deleted a few more
in this one. I left a few behind because the remaining ones would
require additional refactoring that feels outside the scope of this PR.
* Remove getLazyComponentTypeIfResolved
* Return baseProps instead of null
The caller compares the result to baseProps to see if anything changed.
* Avoid redundant checks by inlining getFiberTagFromObjectType
* Move tag resolution to ReactFiber module
* Pass next props to update* functions
We should do this with all types of work in the future.
* Refine component type before pushing/popping context
Removes unnecessary checks.
* Replace all occurrences of _reactResult with helper
* Move shared thenable logic to `shared` package
* Check type of wrapper object before resolving to `default` export
* Return resolved tag instead of reassigning
2018-08-17 00:21:59 +08:00
|
|
|
}
|
2018-04-13 00:39:05 +08:00
|
|
|
}
|
2017-01-25 22:49:19 +08:00
|
|
|
return null;
|
2016-10-30 07:43:34 +08:00
|
|
|
}
|