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
|
|
|
*/
|
|
|
|
|
|
2018-10-19 10:57:12 +08:00
|
|
|
import type {LazyComponent} from 'shared/ReactLazyComponent';
|
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-07-17 05:31:59 +08:00
|
|
|
import warningWithoutStack from 'shared/warningWithoutStack';
|
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,
|
2017-12-13 07:04:40 +08:00
|
|
|
} from 'shared/ReactSymbols';
|
2018-10-19 10:57:12 +08:00
|
|
|
import {refineResolvedLazyComponent} from 'shared/ReactLazyComponent';
|
2017-12-13 07:04:40 +08:00
|
|
|
|
2018-10-12 10:15:14 +08:00
|
|
|
function getWrappedName(
|
|
|
|
|
outerType: mixed,
|
|
|
|
|
innerType: any,
|
|
|
|
|
wrapperName: string,
|
|
|
|
|
): string {
|
|
|
|
|
const functionName = innerType.displayName || innerType.name || '';
|
|
|
|
|
return (
|
|
|
|
|
(outerType: any).displayName ||
|
|
|
|
|
(functionName !== '' ? `${wrapperName}(${functionName})` : wrapperName)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-12 22:32:06 +08:00
|
|
|
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') {
|
2018-07-17 05:31:59 +08:00
|
|
|
warningWithoutStack(
|
2018-07-12 22:32:06 +08:00
|
|
|
false,
|
|
|
|
|
'Received an unexpected object in getComponentName(). ' +
|
|
|
|
|
'This is likely a bug in React. Please file an issue.',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-13 07:04:40 +08:00
|
|
|
if (typeof type === 'function') {
|
2018-07-12 22:32:06 +08:00
|
|
|
return type.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:
|
2018-07-12 22:32:06 +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';
|
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:
|
|
|
|
|
return 'Context.Consumer';
|
|
|
|
|
case REACT_PROVIDER_TYPE:
|
|
|
|
|
return '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:
|
2018-10-20 15:32:16 +08:00
|
|
|
return getComponentName(type.type);
|
2018-10-19 10:57:12 +08:00
|
|
|
case REACT_LAZY_TYPE: {
|
|
|
|
|
const thenable: LazyComponent<mixed> = (type: any);
|
|
|
|
|
const resolvedThenable = refineResolvedLazyComponent(thenable);
|
|
|
|
|
if (resolvedThenable) {
|
|
|
|
|
return getComponentName(resolvedThenable);
|
|
|
|
|
}
|
2019-04-03 19:24:25 +08:00
|
|
|
break;
|
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
2017-11-03 03:50:03 +08:00
|
|
|
export default getComponentName;
|