Skip to content

Commit 3d00479

Browse files
committed
perf: skipping re-rendering
1 parent 812e53e commit 3d00479

File tree

4 files changed

+52
-40
lines changed

4 files changed

+52
-40
lines changed

src/Filler.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,19 @@ const Filler = React.forwardRef(
4646
};
4747
}
4848

49+
const handleResize = React.useCallback(
50+
({ offsetHeight }) => {
51+
if (offsetHeight && onInnerResize) {
52+
onInnerResize();
53+
}
54+
},
55+
[onInnerResize],
56+
);
57+
4958
return (
5059
<div style={outerStyle}>
5160
<ResizeObserver
52-
onResize={({ offsetHeight }) => {
53-
if (offsetHeight && onInnerResize) {
54-
onInnerResize();
55-
}
56-
}}
61+
onResize={handleResize}
5762
>
5863
<div
5964
style={innerStyle}

src/List.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,11 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
102102
[itemKey],
103103
);
104104

105-
const sharedConfig: SharedConfig<T> = {
106-
getKey,
107-
};
105+
const sharedConfig: SharedConfig<T> = React.useMemo(() => {
106+
return {
107+
getKey,
108+
};
109+
}, [getKey]);
108110

109111
// ================================ Scroll ================================
110112
function syncScrollTop(newTop: number | ((prev: number) => number)) {

src/hooks/useChildren.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@ export default function useChildren<T>(
1010
renderFunc: RenderFunc<T>,
1111
{ getKey }: SharedConfig<T>,
1212
) {
13-
return list.slice(startIndex, endIndex + 1).map((item, index) => {
14-
const eleIndex = startIndex + index;
15-
const node = renderFunc(item, eleIndex, {
16-
// style: status === 'MEASURE_START' ? { visibility: 'hidden' } : {},
17-
}) as React.ReactElement;
13+
return React.useMemo(() => {
14+
return list.slice(startIndex, endIndex + 1).map((item, index) => {
15+
const eleIndex = startIndex + index;
16+
const node = renderFunc(item, eleIndex, {
17+
// style: status === 'MEASURE_START' ? { visibility: 'hidden' } : {},
18+
}) as React.ReactElement;
1819

19-
const key = getKey(item);
20-
return (
21-
<Item key={key} setRef={ele => setNodeRef(item, ele)}>
22-
{node}
23-
</Item>
24-
);
25-
});
20+
const key = getKey(item);
21+
return (
22+
<Item key={key} setRef={(ele) => setNodeRef(item, ele)}>
23+
{node}
24+
</Item>
25+
);
26+
});
27+
}, [list, startIndex, endIndex, setNodeRef, renderFunc, getKey]);
2628
}

src/hooks/useHeights.tsx

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)