Skip to content

Commit

Permalink
perf(useVirtual): mutate inner element styles only when items are upd…
Browse files Browse the repository at this point in the history
…ated
  • Loading branch information
wellyshen committed Jul 4, 2021
1 parent 8e858cf commit 70d04ac
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/tall-gifts-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-virtual": patch
---

perf(useVirtual): mutate inner element styles only when items are updated
27 changes: 14 additions & 13 deletions src/useVirtual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,9 @@ export default <
return;
}

const { oStart, oStop, vStart, vStop, margin, innerSize } =
getCalcData(scrollOffset);

innerRef.current.style[marginKey] = `${margin}px`;
innerRef.current.style[sizeKey] = `${innerSize}px`;

const calcData = getCalcData(scrollOffset);
const { oStart, oStop, vStart, vStop } = calcData;
let { margin, innerSize } = calcData;
const nextItems: Item[] = [];
const stickies = Array.isArray(stickyIndicesRef.current)
? stickyIndicesRef.current
Expand Down Expand Up @@ -406,16 +403,20 @@ export default <
measureRef: () => null,
});

innerRef.current.style[marginKey] = `${margin - size}px`;
innerRef.current.style[sizeKey] = `${innerSize + size}px`;
margin -= size;
innerSize += size;
}
}

setItems((prevItems) =>
shouldUpdate(prevItems, nextItems, { measureRef: true })
? nextItems
: prevItems
);
setItems((prevItems) => {
if (shouldUpdate(prevItems, nextItems, { measureRef: true })) {
innerRef.current!.style[marginKey] = `${margin}px`;
innerRef.current!.style[sizeKey] = `${innerSize}px`;
return nextItems;
}

return prevItems;
});

if (!isScrolling) return;

Expand Down

0 comments on commit 70d04ac

Please sign in to comment.