Skip to content

Commit d40e470

Browse files
authored
fix: infinite update loop with zero measured value (#45)
1 parent a84ad34 commit d40e470

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ export function useVirtual({
5757
const reversedMeasurements = []
5858

5959
for (let i = 0, j = size - 1; i < size; i++, j--) {
60+
const measuredSize = measuredCache[i]
6061
const start = measurements[i - 1] ? measurements[i - 1].end : paddingStart
61-
const size = measuredCache[i] || estimateSize(i)
62+
const size =
63+
typeof measuredSize === 'number' ? measuredSize : estimateSize(i)
6264
const end = start + size
6365
const bounds = { index: i, start, size, end }
6466
measurements[i] = {

src/tests/index.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,47 @@ describe('useVirtual', () => {
8787
rendered.getByText('Row 1')
8888
})
8989

90+
it('should render given dynamic size', async () => {
91+
function App() {
92+
const parentRef = React.useRef()
93+
94+
const rowVirtualizer = useVirtual({
95+
size: 20,
96+
parentRef,
97+
overscan: 5,
98+
})
99+
100+
return (
101+
<>
102+
<Container ref={parentRef}>
103+
<Inner
104+
style={{
105+
height: `${rowVirtualizer.totalSize}px`,
106+
}}
107+
>
108+
{rowVirtualizer.virtualItems.map(virtualRow => (
109+
<Row
110+
key={virtualRow.index}
111+
ref={virtualRow.measureRef}
112+
style={{
113+
height: `${virtualRow.size}px`,
114+
transform: `translateY(${virtualRow.start}px)`,
115+
}}
116+
>
117+
Row {virtualRow.index}
118+
</Row>
119+
))}
120+
</Inner>
121+
</Container>
122+
</>
123+
)
124+
}
125+
126+
const rendered = render(<App />)
127+
128+
rendered.getByText('Row 1')
129+
})
130+
90131
// it('scrolling utilities should work', async () => {
91132
// function App() {
92133
// const parentRef = React.useRef()

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,11 +1062,6 @@
10621062
estree-walker "^1.0.1"
10631063
picomatch "^2.2.2"
10641064

1065-
"@scarf/scarf@^1.0.0":
1066-
version "1.0.4"
1067-
resolved "https://registry.yarnpkg.com/@scarf/scarf/-/scarf-1.0.4.tgz#d7c09e7d38428123df18a8d83a4bb5d09517d952"
1068-
integrity sha512-lkhjzeYyYAG4VvdrjvbZCOYzXH5vCwfzYj9xJ4zHDgyYIOzObZwcsbW6W1q5Z4tywrb14oG/tfsFAMMQPCTFqw==
1069-
10701065
"@sheerun/mutationobserver-shim@^0.3.2":
10711066
version "0.3.3"
10721067
resolved "https://registry.yarnpkg.com/@sheerun/mutationobserver-shim/-/mutationobserver-shim-0.3.3.tgz#5405ee8e444ed212db44e79351f0c70a582aae25"

0 commit comments

Comments
 (0)