Skip to content

Commit

Permalink
fix: items not updated when itemCount changed
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyshen committed Jun 5, 2021
1 parent b4a9fce commit bf877cd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-eggs-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-virtual": patch
---

fix: items not updated when `itemCount` changed
24 changes: 12 additions & 12 deletions src/useVirtual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ export default <

if (hasDynamicSizeRef.current) {
while (
vStart < msData.length &&
vStart < itemCount &&
msData[vStart].start < (msData[vStart + 1]?.start || 0) &&
msData[vStart].start + msData[vStart].size < scrollOffset
)
vStart += 1;
} else {
vStart = findNearestBinarySearch(
0,
msData.length - 1,
itemCount - 1,
scrollOffset,
(idx) => msData[idx].start
);
Expand All @@ -145,19 +145,19 @@ export default <
let currStart = msData[vStop].start;

while (
vStop < msData.length &&
vStop < itemCount &&
currStart < scrollOffset + outerRectRef.current[sizeKey]
) {
currStart += msData[vStop].size;
vStop += 1;
}

const oStart = Math.max(vStart - overscanCount, 0);
const oStop = Math.min(vStop + overscanCount, msData.length) - 1;
const oStop = Math.min(vStop + overscanCount, itemCount) - 1;
const margin = msData[oStart].start;
const lastStart = Math[oStop < msData.length - 1 ? "max" : "min"](
const lastStart = Math[oStop < itemCount - 1 ? "max" : "min"](
msData[oStop].end + msData[oStop].size,
msData[msData.length - 1].start
msData[itemCount - 1].start
);

return {
Expand All @@ -169,7 +169,7 @@ export default <
innerSize: lastStart - margin,
};
},
[overscanCount, sizeKey]
[itemCount, overscanCount, sizeKey]
);

const [resetIsScrolling, cancelResetIsScrolling] = useDebounce(
Expand Down Expand Up @@ -231,7 +231,7 @@ export default <
// see: https://caniuse.com/mdn-api_resizeobserverentry_borderboxsize
const measuredSize = target.getBoundingClientRect()[sizeKey];

if (!outerRef.current || !measuredSize) {
if (!measuredSize) {
ro.disconnect();
rosRef.current.delete(target);
return;
Expand All @@ -241,7 +241,7 @@ export default <

if (measuredSize !== size || start !== prevEnd) {
if (i < prevItemIdxRef.current && start < scrollOffset)
outerRef.current[scrollKey] =
outerRef.current![scrollKey] =
scrollOffset + measuredSize - size;

msDataRef.current[i] = getMeasure(i, measuredSize);
Expand Down Expand Up @@ -418,14 +418,14 @@ export default <
const { width, height } = outerRectRef.current;
const isSameW = width === rect.width;
const isSameS = isSameW && height === rect.height;
const prevTotalS = msDataRef.current[msDataRef.current.length - 1]?.end;
const prevTotalS = msDataRef.current[itemCount - 1]?.end;

outerRectRef.current = rect;
measureItems(hasDynamicSizeRef.current || isSameS);
measureItems(hasDynamicSizeRef.current);
handleScroll(scrollOffsetRef.current);

if (!hasDynamicSizeRef.current && !isSameW) {
const totalS = msDataRef.current[msDataRef.current.length - 1]?.end;
const totalS = msDataRef.current[itemCount - 1]?.end;
const ratio = totalS / prevTotalS || 1;

outerRef.current[scrollKey] = scrollOffsetRef.current * ratio;
Expand Down

0 comments on commit bf877cd

Please sign in to comment.