1
- import { supportRef } from 'rc-util/lib/ref' ;
1
+ import { supportRef } from '@ rc-component/ util/lib/ref' ;
2
2
import * as React from 'react' ;
3
3
4
4
export type CompareProps < T extends React . ComponentType < any > > = (
5
5
prevProps : Readonly < React . ComponentProps < T > > ,
6
6
nextProps : Readonly < React . ComponentProps < T > > ,
7
7
) => boolean ;
8
8
9
+ type ImmutableProps < T extends React . ComponentType < any > > = Omit < React . ComponentProps < T > , 'ref' > ;
10
+
9
11
/**
10
12
* Create Immutable pair for `makeImmutable` and `responseImmutable`.
11
13
*/
@@ -32,24 +34,24 @@ export default function createImmutable() {
32
34
function makeImmutable < T extends React . ComponentType < any > > (
33
35
Component : T ,
34
36
shouldTriggerRender ?: CompareProps < T > ,
35
- ) : T {
37
+ ) : React . ComponentType < React . ComponentProps < T > > {
36
38
const refAble = supportRef ( Component ) ;
37
39
38
- const ImmutableComponent = function ( props : any , ref : any ) {
40
+ const ImmutableComponent = ( props : ImmutableProps < T > , ref : React . Ref < any > ) => {
39
41
const refProps = refAble ? { ref } : { } ;
40
42
const renderTimesRef = React . useRef ( 0 ) ;
41
43
const prevProps = React . useRef ( props ) ;
42
44
43
45
// If parent has the context, we do not wrap it
44
46
const mark = useImmutableMark ( ) ;
45
47
if ( mark !== null ) {
46
- return < Component { ...props } { ...refProps } /> ;
48
+ return < Component { ...( props as any ) } { ...refProps } /> ;
47
49
}
48
50
49
51
if (
50
- // Always trigger re-render if not provide `notTriggerRender`
52
+ // Always trigger re-render if `shouldTriggerRender` is not provided
51
53
! shouldTriggerRender ||
52
- shouldTriggerRender ( prevProps . current , props )
54
+ shouldTriggerRender ( prevProps . current as any , props as any )
53
55
) {
54
56
renderTimesRef . current += 1 ;
55
57
}
@@ -58,7 +60,7 @@ export default function createImmutable() {
58
60
59
61
return (
60
62
< ImmutableContext . Provider value = { renderTimesRef . current } >
61
- < Component { ...props } { ...refProps } />
63
+ < Component { ...( props as any ) } { ...refProps } />
62
64
</ ImmutableContext . Provider >
63
65
) ;
64
66
} ;
@@ -67,7 +69,9 @@ export default function createImmutable() {
67
69
ImmutableComponent . displayName = `ImmutableRoot(${ Component . displayName || Component . name } )` ;
68
70
}
69
71
70
- return refAble ? React . forwardRef ( ImmutableComponent ) : ( ImmutableComponent as any ) ;
72
+ return refAble
73
+ ? ( React . forwardRef ( ImmutableComponent ) as React . ComponentType < React . ComponentProps < T > > )
74
+ : ( ImmutableComponent as unknown as React . ComponentType < React . ComponentProps < T > > ) ;
71
75
}
72
76
73
77
/**
@@ -77,14 +81,13 @@ export default function createImmutable() {
77
81
function responseImmutable < T extends React . ComponentType < any > > (
78
82
Component : T ,
79
83
propsAreEqual ?: CompareProps < T > ,
80
- ) : T {
84
+ ) : React . ComponentType < React . ComponentProps < T > > {
81
85
const refAble = supportRef ( Component ) ;
82
86
83
- const ImmutableComponent = function ( props : any , ref : any ) {
87
+ const ImmutableComponent = ( props : ImmutableProps < T > , ref : React . Ref < any > ) => {
84
88
const refProps = refAble ? { ref } : { } ;
85
89
useImmutableMark ( ) ;
86
-
87
- return < Component { ...props } { ...refProps } /> ;
90
+ return < Component { ...( props as any ) } { ...refProps } /> ;
88
91
} ;
89
92
90
93
if ( process . env . NODE_ENV !== 'production' ) {
@@ -94,8 +97,12 @@ export default function createImmutable() {
94
97
}
95
98
96
99
return refAble
97
- ? React . memo ( React . forwardRef ( ImmutableComponent ) , propsAreEqual )
98
- : ( React . memo ( ImmutableComponent , propsAreEqual ) as any ) ;
100
+ ? ( React . memo ( React . forwardRef ( ImmutableComponent ) , propsAreEqual ) as React . ComponentType <
101
+ React . ComponentProps < T >
102
+ > )
103
+ : ( React . memo ( ImmutableComponent , propsAreEqual ) as unknown as React . ComponentType <
104
+ React . ComponentProps < T >
105
+ > ) ;
99
106
}
100
107
101
108
return {
0 commit comments