Skip to content

Commit 7cfb93d

Browse files
committed
perf: skipping re-rendering
1 parent d2bc792 commit 7cfb93d

File tree

4 files changed

+52
-42
lines changed

4 files changed

+52
-42
lines changed

src/Filler.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,18 @@ const Filler = React.forwardRef(
4242
};
4343
}
4444

45+
const handleResize = React.useCallback(
46+
({ offsetHeight }) => {
47+
if (offsetHeight && onInnerResize) {
48+
onInnerResize();
49+
}
50+
},
51+
[onInnerResize],
52+
);
53+
4554
return (
4655
<div style={outerStyle}>
47-
<ResizeObserver
48-
onResize={({ offsetHeight }) => {
49-
if (offsetHeight && onInnerResize) {
50-
onInnerResize();
51-
}
52-
}}
53-
>
56+
<ResizeObserver onResize={handleResize}>
5457
<div
5558
style={innerStyle}
5659
className={classNames({

src/List.tsx

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

100-
const sharedConfig: SharedConfig<T> = {
101-
getKey,
102-
};
100+
const sharedConfig: SharedConfig<T> = React.useMemo(() => {
101+
return {
102+
getKey,
103+
};
104+
}, [getKey]);
103105

104106
// ================================ Scroll ================================
105107
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)