Yes, yet another React Virtual List... Why?
- Provides dynamic item heights
- Only measures what it draws, estimates the rest and keeps everything smooth by adjusting scroll position when sizes are discovered later
- Does not need to measure intermediate items when large scrolling, massively improving performance
- Does not need an accurate estimated item height
- Allows items to change size at any time
- Allows natural browser layout of the components on screen within their standard container, no individual item positioning
- Works in environments that don't constantly fire scroll events (e.g. iOS)
This component uses a O(Ln2) algorithm to work out the position of items and caches all of this for maximum performance even when scrolling huge distances.
Supports list of up to 1,000,000 pixels in height (due to browser limitations on pixel heights). Provides events that enable any number of items.
npm i react-virtual-dynamic-list
<Virtual items={someCollection} renderItem={item=>(<div>{item.id}</div>)}/>
or
<Virtual items={100000} renderItem={item=>(<div>Item number {item + 1}</div>)}/>
Provides the items that will be rendered, if an array is used, the contents are passed to the renderItem function as context, otherwise the index is passed
A function to render the item. The first parameter is the item or the item's index. The second is always the index.
If you need your items to render properly inside a wrapper component then you can provide it here.
Your component must apply the style
prop passed to it and render children
If want to provider a component to render the whole of the virtual list you can provide it here.
Your component must apply the style
prop passed to it, take a ref
via forwardRef
and apply it to the root and render children
Animation is used in addition to scrolling. A very minor overhead.
Provides a number of pages of overscan
Heights are worked out from averages after the first render, so something rough is fine.
Provides access to some api functions that can be useful for modifying the list. Often you will cache these for later use.
Provides useful information on item sizes so you can resize the component, the most useful being averageHeight of an item based on the items drawn.
if you like at any time.
Provides an event that can modify the scroll. You may change items
in this function. If
you insert things above you are probably going to have to update the scroll position
afterwards.
you might want to fiddle with scrollTop when you add things (especially if going upwards)
function onScroll({max, items}) {
if (max > items.length - 15) {
items.push(...Array.from({length: 15}, (_, index) => ({
id: index + items.length,
height: Math.random() * 98 + 32 | 0,
color: rgb(Math.random() * 112 + 143,
Math.random() * 112 + 143, Math.random() * 112 + 143)
})))
}
}
Are passed to the wrapping div that does the scrolling (this is not Wrapper, that holds the actual items).
<Virtual items={1000} renderItem={i=>(<div>{i}</div>)} width={80} height={200}/>
Sets the size of the rendered div to 80 x 200
This is a component you can use as a Holder for the Virtual component. It uses shadows to indicate that scrolling is possible.
You can pass a shadow parameter throw to it via the Virtual.
<Virtual shadow={'0 0 32px 14px black'} items={[...items]} Holder={ScrollIndicatorHolder} renderItem={item => {
return <Item item={item}/>
}}/>
Provides the ability to measure a component using a resizeObserver - so it will redraw on resize too which is handy. ResizeObserver is pony filled.
const [size, ref] = useMeasurement()
return <div ref={ref}>Something is {size.width} x {size.height} at {size.left}, {size.top} </div>