@@ -15,11 +15,11 @@ export default function useHeights<T>(
15
15
const heightsRef = useRef ( new CacheMap ( ) ) ;
16
16
const collectRafRef = useRef < number > ( ) ;
17
17
18
- function cancelRaf ( ) {
18
+ const cancelRaf = React . useCallback ( function cancelRaf ( ) {
19
19
raf . cancel ( collectRafRef . current ) ;
20
- }
20
+ } , [ ] ) ;
21
21
22
- function collectHeight ( ) {
22
+ const collectHeight = React . useCallback ( function ( ) {
23
23
cancelRaf ( ) ;
24
24
25
25
collectRafRef . current = raf ( ( ) => {
@@ -36,28 +36,31 @@ export default function useHeights<T>(
36
36
// Always trigger update mark to tell parent that should re-calculate heights when resized
37
37
setUpdatedMark ( ( c ) => c + 1 ) ;
38
38
} ) ;
39
- }
40
-
41
- function setInstanceRef ( item : T , instance : HTMLElement ) {
42
- const key = getKey ( item ) ;
43
- const origin = instanceRef . current . get ( key ) ;
39
+ } , [ ] ) ;
44
40
45
- if ( instance ) {
46
- instanceRef . current . set ( key , instance ) ;
47
- collectHeight ( ) ;
48
- } else {
49
- instanceRef . current . delete ( key ) ;
50
- }
41
+ const setInstanceRef = React . useCallback (
42
+ function ( item : T , instance : HTMLElement ) {
43
+ const key = getKey ( item ) ;
44
+ const origin = instanceRef . current . get ( key ) ;
51
45
52
- // Instance changed
53
- if ( ! origin !== ! instance ) {
54
46
if ( instance ) {
55
- onItemAdd ?.( item ) ;
47
+ instanceRef . current . set ( key , instance ) ;
48
+ collectHeight ( ) ;
56
49
} else {
57
- onItemRemove ?.( item ) ;
50
+ instanceRef . current . delete ( key ) ;
51
+ }
52
+
53
+ // Instance changed
54
+ if ( ! origin !== ! instance ) {
55
+ if ( instance ) {
56
+ onItemAdd ?.( item ) ;
57
+ } else {
58
+ onItemRemove ?.( item ) ;
59
+ }
58
60
}
59
- }
60
- }
61
+ } ,
62
+ [ getKey , onItemAdd , onItemRemove ] ,
63
+ ) ;
61
64
62
65
useEffect ( ( ) => {
63
66
return cancelRaf ;
0 commit comments