Skip to content

Commit 364c071

Browse files
aehmtpetyosi
andauthored
fix: clamp initialItemCount to data length when data prop is used (#1461)
* fix: clamp initialItemCount to data length when data prop is used When initialItemCount exceeds data.length, buildListStateFromItemCount creates phantom items with data: undefined. These phantom items produce zero-height DOM elements, triggering Zero-sized element console errors. Clamp the adjusted count to data.length when the data prop is provided (non-empty array), so Virtuoso never renders more initial items than exist in the data array. When data is not used (totalCount-only mode), the existing behavior is preserved. Relates to #1060 -- the prior fix (PR #1061) addressed a ResizeObserver timing issue in useSize, but this separate code path in buildListStateFromItemCount was never patched. * chore: add changeset for initialItemCount clamp fix * fix: prevent phantom items in all initialItemCount code paths Follow-up to the data.length clamp, covering the remaining gaps: - clamp the count to the data remaining after initialTopMostItemIndex, which could still read past the end of the array - treat an explicitly empty data array as zero items, while omitted data keeps the index-driven initial rendering - add initialItemCount to the list state combineLatest so publishing it recomputes immediately; previously it was read from a stale cell and a data shrink in the same render cycle produced undefined items, crashing computeItemKey in unmeasured (hidden) viewports - add a 13-input combineLatest overload to urx --------- Co-authored-by: Petyo Ivanov <[email protected]>
1 parent dad47e0 commit 364c071

5 files changed

Lines changed: 119 additions & 8 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-virtuoso': patch
3+
---
4+
5+
Fixed "Zero-sized element, this should not happen" error when `initialItemCount` exceeds the available `data`. The initial list state builder now clamps the item count to the data remaining after `initialTopMostItemIndex`, and renders no items when `data` is explicitly empty, preventing phantom items with `undefined` data from being rendered. `initialItemCount` also became a reactive input of the list state computation, so changing it after mount recomputes the list instead of leaving stale state until an unrelated update.

‎packages/react-virtuoso/src/initialItemCountSystem.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const initialItemCountSystem = u.system(
1212
u.withLatestFrom(initialItemCount),
1313
u.filter(([, count]) => count !== 0),
1414
u.withLatestFrom(initialTopMostItemIndex, sizes, firstItemIndex, gap, data),
15-
u.map(([[, count], initialTopMostItemIndexValue, sizes, firstItemIndex, gap, data = []]) => {
15+
u.map(([[, count], initialTopMostItemIndexValue, sizes, firstItemIndex, gap, data]) => {
1616
return buildListStateFromItemCount(count, initialTopMostItemIndexValue, sizes, firstItemIndex, gap, data)
1717
})
1818
),

‎packages/react-virtuoso/src/listStateSystem.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export function buildListStateFromItemCount(
9797
sizes: SizeState,
9898
firstItemIndex: number,
9999
gap: number,
100-
data: readonly unknown[]
100+
data: readonly unknown[] | undefined
101101
) {
102102
let includedGroupsCount = 0
103103
if (sizes.groupIndices.length > 0) {
@@ -109,11 +109,16 @@ export function buildListStateFromItemCount(
109109
}
110110
}
111111

112-
const adjustedCount = itemCount + includedGroupsCount
113-
const initialTopMostItemIndexNumber = getInitialTopMostItemIndexNumber(initialTopMostItemIndex, adjustedCount)
112+
const unadjustedCount = itemCount + includedGroupsCount
113+
const hasData = data !== undefined
114+
const dataLength = data?.length ?? 0
115+
const initialTopMostItemIndexNumber = getInitialTopMostItemIndexNumber(initialTopMostItemIndex, hasData ? dataLength : unadjustedCount)
116+
// when the data prop is set, items are read from it starting at initialTopMostItemIndexNumber,
117+
// so the count cannot exceed the items remaining after it - an explicitly empty data renders no items
118+
const adjustedCount = hasData ? Math.max(0, Math.min(unadjustedCount, dataLength - initialTopMostItemIndexNumber)) : unadjustedCount
114119

115120
const items = Array.from({ length: adjustedCount }).map((_, index) => ({
116-
data: data[index + initialTopMostItemIndexNumber],
121+
data: data?.[index + initialTopMostItemIndexNumber],
117122
index: index + initialTopMostItemIndexNumber,
118123
offset: 0,
119124
size: 0,
@@ -209,7 +214,8 @@ export const listStateSystem = u.system(
209214
u.duc(firstItemIndex),
210215
u.duc(gap),
211216
u.duc(minOverscanItemCount),
212-
data
217+
data,
218+
u.duc(initialItemCount)
213219
),
214220
u.filter(([mount, recalcInProgress, , totalCount, , , , , , , , data]) => {
215221
// When data length changes, it is synced to totalCount, both of which trigger a recalc separately.
@@ -231,10 +237,10 @@ export const listStateSystem = u.system(
231237
gap,
232238
minOverscanItemCountValue,
233239
data,
240+
initialItemCountValue,
234241
]) => {
235242
const sizesValue = sizes
236243
const { offsetTree, sizeTree } = sizesValue
237-
const initialItemCountValue = u.getValue(initialItemCount)
238244

239245
if (totalCount === 0) {
240246
return { ...EMPTY_LIST_STATE, totalCount }
@@ -245,7 +251,7 @@ export const listStateSystem = u.system(
245251
if (initialItemCountValue === 0) {
246252
return { ...EMPTY_LIST_STATE, totalCount }
247253
}
248-
return buildListStateFromItemCount(initialItemCountValue, initialTopMostItemIndex, sizes, firstItemIndex, gap, data || [])
254+
return buildListStateFromItemCount(initialItemCountValue, initialTopMostItemIndex, sizes, firstItemIndex, gap, data)
249255
}
250256

251257
if (empty(sizeTree)) {

‎packages/react-virtuoso/src/urx/transformers.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,23 @@ export function combineLatest<O1, O2, O3, O4, O5, O6, O7, O8, O9, O10, O11, O12>
9797
Emitter<O12>,
9898
]
9999
): Emitter<[O1, O2, O3, O4, O5, O6, O7, O8, O9, O10, O11, O12]> // prettier-ignore
100+
export function combineLatest<O1, O2, O3, O4, O5, O6, O7, O8, O9, O10, O11, O12, O13>(
101+
...emitters: [
102+
Emitter<O1>,
103+
Emitter<O2>,
104+
Emitter<O3>,
105+
Emitter<O4>,
106+
Emitter<O5>,
107+
Emitter<O6>,
108+
Emitter<O7>,
109+
Emitter<O8>,
110+
Emitter<O9>,
111+
Emitter<O10>,
112+
Emitter<O11>,
113+
Emitter<O12>,
114+
Emitter<O13>,
115+
]
116+
): Emitter<[O1, O2, O3, O4, O5, O6, O7, O8, O9, O10, O11, O12, O13]> // prettier-ignore
100117
export function combineLatest(...emitters: Emitter<any>[]): Emitter<any> {
101118
const innerSubject = stream<any>()
102119
const values = Array.from({ length: emitters.length })

‎packages/react-virtuoso/test/listSystem.test.ts‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,89 @@ describe('list engine', () => {
4545
expect(getValue(listState).items).toHaveLength(10)
4646
})
4747

48+
it('clamps initialItemCount to data length when data has fewer items', () => {
49+
const { data, initialItemCount, listState, propsReady } = init(listSystem)
50+
51+
publish(data, ['a', 'b', 'c'])
52+
publish(initialItemCount, 10)
53+
publish(propsReady, true)
54+
55+
const items = getValue(listState).items
56+
expect(items).toHaveLength(3)
57+
expect(items.every((item) => item.data !== undefined)).toBe(true)
58+
})
59+
60+
it('does not render phantom items when data shrinks while initialItemCount is stale', () => {
61+
const { data, initialItemCount, listState, propsReady } = init(listSystem)
62+
63+
publish(data, ['a', 'b', 'c'])
64+
publish(initialItemCount, 3)
65+
publish(propsReady, true)
66+
expect(getValue(listState).items).toHaveLength(3)
67+
68+
// same-render re-publish: data shrinks first, before the new initialItemCount lands in its cell
69+
publish(data, ['a', 'b'])
70+
let items = getValue(listState).items
71+
expect(items).toHaveLength(2)
72+
expect(items.every((item) => item.data !== undefined)).toBe(true)
73+
74+
publish(initialItemCount, 2)
75+
items = getValue(listState).items
76+
expect(items).toHaveLength(2)
77+
expect(items.every((item) => item.data !== undefined)).toBe(true)
78+
})
79+
80+
it('recomputes the list state when initialItemCount changes after data changes', () => {
81+
const { data, initialItemCount, listState, propsReady } = init(listSystem)
82+
83+
publish(data, ['a', 'b', 'c'])
84+
publish(initialItemCount, 3)
85+
publish(propsReady, true)
86+
expect(getValue(listState).items).toHaveLength(3)
87+
88+
publish(data, ['a', 'b', 'c', 'd', 'e'])
89+
expect(getValue(listState).items).toHaveLength(3)
90+
91+
publish(initialItemCount, 5)
92+
const items = getValue(listState).items
93+
expect(items).toHaveLength(5)
94+
expect(items.every((item) => item.data !== undefined)).toBe(true)
95+
})
96+
97+
it('clamps initialItemCount to the data remaining after initialTopMostItemIndex', () => {
98+
const { data, initialItemCount, initialTopMostItemIndex, listState, propsReady } = init(listSystem)
99+
100+
publish(data, ['a', 'b', 'c'])
101+
publish(initialTopMostItemIndex, 1)
102+
publish(initialItemCount, 3)
103+
publish(propsReady, true)
104+
105+
const items = getValue(listState).items
106+
expect(items).toHaveLength(2)
107+
expect(items.map((item) => item.data)).toEqual(['b', 'c'])
108+
})
109+
110+
it('renders no items on mount when data is explicitly empty', () => {
111+
const { data, initialItemCount, listState, propsReady } = init(listSystem)
112+
113+
publish(data, [])
114+
publish(initialItemCount, 3)
115+
publish(propsReady, true)
116+
117+
expect(getValue(listState).items).toHaveLength(0)
118+
})
119+
120+
it('renders initialItemCount items on mount when data is omitted', () => {
121+
const { initialItemCount, listState, propsReady } = init(listSystem)
122+
123+
publish(initialItemCount, 3)
124+
publish(propsReady, true)
125+
126+
const items = getValue(listState).items
127+
expect(items).toHaveLength(3)
128+
expect(items.every((item) => item.data === undefined)).toBe(true)
129+
})
130+
48131
it('returns the full set if a fixed item height is set', () => {
49132
const { fixedItemHeight, listState, propsReady, scrollTop, totalCount, viewportHeight } = init(listSystem)
50133

0 commit comments

Comments
 (0)