Skip to content

Commit

Permalink
fix: resolve flow type error
Browse files Browse the repository at this point in the history
  • Loading branch information
xyy94813 committed Jul 16, 2021
1 parent adb1b4f commit 08970b0
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/libs/getComponentNameFromType.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @flow
*/

import type { LazyComponent, ReactContext, ReactProviderType } from 'react';
import type { Context, StatelessFunctionalComponent } from 'react';
import { Fragment } from 'react';
import {
ContextConsumer,
Expand All @@ -20,6 +20,16 @@ import {
Lazy,
} from 'react-is';

/**
* didn't export the type in React
* same as https://github.com/facebook/react/blob/310187264d01a31bc3079358f13662d31a079d9e/packages/react/index.js
*/
type LazyComponent<T, P> = {
$$typeof: Symbol | number,
_payload: P,
_init: (payload: P) => T,
};

// Keep in sync with react-reconciler/getComponentNameFromFiber
function getWrappedName(
outerType: mixed,
Expand All @@ -35,7 +45,7 @@ function getWrappedName(
}

// Keep in sync with react-reconciler/getComponentNameFromFiber
function getContextName(type: ReactContext<any>) {
function getContextName(type: Context<any>) {
return type.displayName || 'Context';
}

Expand Down Expand Up @@ -72,23 +82,28 @@ function getComponentNameFromType(type: mixed): string | null {
if (typeof type === 'object') {
// eslint-disable-next-line default-case
switch (type.$$typeof) {
case ContextConsumer:
case ContextConsumer: {
/**
* in DEV, should get context from `_context`.
* https://github.com/facebook/react/blob/e16d61c3000e2de6217d06b9afad162e883f73c4/packages/react/src/ReactContext.js#L44-L125
*/
return `${getContextName(type._context ?? type)}.Consumer`;
case ContextProvider:
return `${getContextName(type._context)}.Provider`;
const context: any = type._context ?? type;
return `${getContextName(context)}.Consumer`;
}
case ContextProvider: {
const context: any = type._context;
return `${getContextName(context)}.Provider`;
}
case ForwardRef:
// eslint-disable-next-line no-case-declarations
return getWrappedName(type, type.render, 'ForwardRef');
case Memo:
case Memo: {
const outerName = (type: any).displayName || null;
if (outerName !== null) {
return outerName;
}
return getComponentNameFromType(type.type) || 'Memo';
}
case Lazy: {
const lazyComponent: LazyComponent<any, any> = (type: any);
const payload = lazyComponent._payload;
Expand Down

0 comments on commit 08970b0

Please sign in to comment.