4
4
* @flow
5
5
*/
6
6
7
- import type { LazyComponent , ReactContext , ReactProviderType } from 'react' ;
7
+ import type { Context , StatelessFunctionalComponent } from 'react' ;
8
8
import { Fragment } from 'react' ;
9
9
import {
10
10
ContextConsumer ,
@@ -20,6 +20,16 @@ import {
20
20
Lazy ,
21
21
} from 'react-is' ;
22
22
23
+ /**
24
+ * didn't export the type in React
25
+ * same as https://github.com/facebook/react/blob/310187264d01a31bc3079358f13662d31a079d9e/packages/react/index.js
26
+ */
27
+ type LazyComponent < T , P > = {
28
+ $$typeof : Symbol | number ,
29
+ _payload : P ,
30
+ _init : ( payload : P ) => T ,
31
+ } ;
32
+
23
33
// Keep in sync with react-reconciler/getComponentNameFromFiber
24
34
function getWrappedName (
25
35
outerType : mixed ,
@@ -35,7 +45,7 @@ function getWrappedName(
35
45
}
36
46
37
47
// Keep in sync with react-reconciler/getComponentNameFromFiber
38
- function getContextName ( type : ReactContext < any > ) {
48
+ function getContextName ( type : Context < any > ) {
39
49
return type . displayName || 'Context' ;
40
50
}
41
51
@@ -72,23 +82,28 @@ function getComponentNameFromType(type: mixed): string | null {
72
82
if ( typeof type === 'object' ) {
73
83
// eslint-disable-next-line default-case
74
84
switch ( type . $$typeof ) {
75
- case ContextConsumer :
85
+ case ContextConsumer : {
76
86
/**
77
87
* in DEV, should get context from `_context`.
78
88
* https://github.com/facebook/react/blob/e16d61c3000e2de6217d06b9afad162e883f73c4/packages/react/src/ReactContext.js#L44-L125
79
89
*/
80
- return `${ getContextName ( type . _context ?? type ) } .Consumer` ;
81
- case ContextProvider :
82
- return `${ getContextName ( type . _context ) } .Provider` ;
90
+ const context : any = type . _context ?? type ;
91
+ return `${ getContextName ( context ) } .Consumer` ;
92
+ }
93
+ case ContextProvider : {
94
+ const context : any = type . _context ;
95
+ return `${ getContextName ( context ) } .Provider` ;
96
+ }
83
97
case ForwardRef :
84
98
// eslint-disable-next-line no-case-declarations
85
99
return getWrappedName ( type , type . render , 'ForwardRef' ) ;
86
- case Memo :
100
+ case Memo : {
87
101
const outerName = ( type : any ) . displayName || null ;
88
102
if ( outerName !== null ) {
89
103
return outerName ;
90
104
}
91
105
return getComponentNameFromType ( type . type ) || 'Memo' ;
106
+ }
92
107
case Lazy : {
93
108
const lazyComponent : LazyComponent < any , any > = ( type : any ) ;
94
109
const payload = lazyComponent . _payload ;
0 commit comments