@@ -15,11 +15,11 @@ export default function useHeights<T>(
1515 const heightsRef = useRef ( new CacheMap ( ) ) ;
1616 const collectRafRef = useRef < number > ( ) ;
1717
18- function cancelRaf ( ) {
18+ const cancelRaf = React . useCallback ( function cancelRaf ( ) {
1919 raf . cancel ( collectRafRef . current ) ;
20- }
20+ } , [ ] ) ;
2121
22- function collectHeight ( ) {
22+ const collectHeight = React . useCallback ( function ( ) {
2323 cancelRaf ( ) ;
2424
2525 collectRafRef . current = raf ( ( ) => {
@@ -36,28 +36,31 @@ export default function useHeights<T>(
3636 // Always trigger update mark to tell parent that should re-calculate heights when resized
3737 setUpdatedMark ( ( c ) => c + 1 ) ;
3838 } ) ;
39- }
40-
41- function setInstanceRef ( item : T , instance : HTMLElement ) {
42- const key = getKey ( item ) ;
43- const origin = instanceRef . current . get ( key ) ;
39+ } , [ ] ) ;
4440
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 ) ;
5145
52- // Instance changed
53- if ( ! origin !== ! instance ) {
5446 if ( instance ) {
55- onItemAdd ?.( item ) ;
47+ instanceRef . current . set ( key , instance ) ;
48+ collectHeight ( ) ;
5649 } 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+ }
5860 }
59- }
60- }
61+ } ,
62+ [ getKey , onItemAdd , onItemRemove ] ,
63+ ) ;
6164
6265 useEffect ( ( ) => {
6366 return cancelRaf ;
0 commit comments