Files
react/packages/shared/getComponentName.js
Andrew Clark 5031ebf6be 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-16 09:21:59 -07:00

89 lines
2.4 KiB
JavaScript

/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* 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 {Thenable} from 'shared/ReactLazyComponent';
import warningWithoutStack from 'shared/warningWithoutStack';
import {
REACT_ASYNC_MODE_TYPE,
REACT_CONTEXT_TYPE,
REACT_FORWARD_REF_TYPE,
REACT_FRAGMENT_TYPE,
REACT_PORTAL_TYPE,
REACT_PROFILER_TYPE,
REACT_PROVIDER_TYPE,
REACT_STRICT_MODE_TYPE,
REACT_PLACEHOLDER_TYPE,
} from 'shared/ReactSymbols';
import {
getResultFromResolvedThenable,
refineResolvedThenable,
} from 'shared/ReactLazyComponent';
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_ASYNC_MODE_TYPE:
return 'AsyncMode';
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_PLACEHOLDER_TYPE:
return 'Placeholder';
}
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:
const renderFn = (type.render: any);
const functionName = renderFn.displayName || renderFn.name || '';
return functionName !== ''
? `ForwardRef(${functionName})`
: 'ForwardRef';
}
if (typeof type.then === 'function') {
const thenable: Thenable<mixed> = (type: any);
const resolvedThenable = refineResolvedThenable(thenable);
if (resolvedThenable) {
const Component = getResultFromResolvedThenable(resolvedThenable);
return getComponentName(Component);
}
}
}
return null;
}
export default getComponentName;