VirtualScroller is a performant approach to handle huge data efficiently.
import { VirtualScroller } from 'primereact/virtualscroller';
<script src="https://unpkg.com/primereact/core/core.min.js"></script>
<script src="https://unpkg.com/primereact/virtualscroller/virtualscroller.min.js"></script>
VirtualScroller is used to display huge data. It periodically adds special elements defined according to the scroll's position to the DOM. The itemSize and itemTemplate properties are required on component. In addition, an initial array is required based on the total number of items to display.
VirtualScroller automatically calculates how many items will be displayed in the view according to itemSize using a specified scroll height. Its scroll height can be adjusted with scrollHeight property or height property of CSS.
<VirtualScroller items={items} itemSize={50} itemTemplate={itemTemplate} />
const items = Array.from({ length: 100000 }).map((_, i) => `Item #${i}`);
const itemTemplate = (item, options) => {
// item: Current item.
// options.index: Index of the item.
// options.count: Total numbers of items.
// options.first: Whether this is the first item.
// options.last: Whether this is the last item.
// options.even: Whether the index is even.
// options.odd: Whether the index is odd.
// options.props: Props of component.
return <div style={{ height: '50px' }}>{item}</div>;
}
VirtualScroller has a special loader. It can be activated with the showLoader property. In addition, loadingTemplate can be used to add custom loaders to item elements.
<VirtualScroller items={items} itemSize={50} itemTemplate={itemTemplate} showLoader delay={250} />
<VirtualScroller items={items} itemSize={50} itemTemplate={itemTemplate} showLoader delay={250} loadingTemplate={loadingTemplate} />
const loadingTemplate = (options) => {
// options.index: Index of the item.
// options.count: Total numbers of items.
// options.first: Whether this is the first item.
// options.last: Whether this is the last item.
// options.even: Whether the index is even.
// options.odd: Whether the index is odd.
// options.props: Props of component.
// options.numCols: Total number of columns in a row in 'both' orientation mode in view.
return (
<div style={{ height: '50px' }}>
<Skeleton width={options.even ? '60%' : '50%'} height="1.3rem" />
</div>
);
}
Lazy mode is handy to deal with large datasets, instead of loading the entire data, small chunks of data is loaded by invoking onLazyLoad callback.
<VirtualScroller items={lazyItems} itemSize={50} itemTemplate={itemTemplate} lazy onLazyLoad={onLazyLoad}
showLoader loading={lazyLoading} />
const onLazyLoad = (event) => {
setLazyLoading(true);
if (loadLazyTimeout) {
clearTimeout(loadLazyTimeout);
}
//imitate delay of a backend call
loadLazyTimeout = setTimeout(() => {
const { first, last } = event;
const _lazyItems = [...lazyItems];
for (let i = first; i < last; i++) {
_lazyItems[i] = `Item #${i}`;
}
setLazyItems(_lazyItems)
setLazyLoading(true);
}, Math.random() * 1000 + 250);
}
VirtualScroller has a HTML div element to wrap the all items. But in some cases, it may be desirable to define a completely special wrapper element instead of the HTML div element. The contentTemplate property can be used for this. This will be especially necessary to maintain the DOM layout and provide accessibility.
<VirtualScroller items={items} itemSize={50} itemTemplate={itemTemplate} contentTemplate={contentTemplate} />
const contentTemplate = (options) => {
// options.className: Class name of wrapper element.
// options.contentRef: Ref of wrapper element.
// options.spacerRef: Ref of spacer element.
// options.stickyRef: Ref of sticky element in content.
// options.items: Loaded data.
// options.getItemOptions(index): Information of any item.
// options.children: Items of wrapper element.
// options.element: Default wrapper element.
// options.props: Props of component.
// options.loading: Whether the data is loaded.
// options.getLoaderOptions(index): Information of any item during the loading.
// options.loadingTemplate: Template of loading item.
// options.itemSize: The height/width of item according to orientation.
// options.vertical: Whether the orientation is vertical.
// options.horizontal: Whether the orientation is horizontal.
// options.both: Whether the orientation is both.
return (
<ul ref={options.contentRef} className={options.className} role="listbox">
{options.children}
</ul>
);
}
const itemTemplate = (item, options) => {
return <li role="option" style={{ height: '50px' }}>{item}</li>
}
Name | Type | Default | Description |
---|---|---|---|
id | string | null | Unique identifier of the element. |
style | object | null | Inline style of the component. |
className | any | null | Style class of the component. |
items | array | null | An array of objects to display. |
itemSize | number / [number, number] | null | The height/width of item according to orientation. |
scrollHeight | string | null | Height of the scroll viewport. |
scrollWidth | string | null | Width of the scroll viewport. |
orientation | string | 'vertical' | The orientation of scrollbar, valid values are 'vertical', 'horizontal' and 'both'. |
numToleratedItems | number | null | Determines how many additional elements to add to the DOM outside of the view. According to the scrolls made up and down, extra items are added in a certain algorithm in the form of multiples of this number. Default value is half the number of items shown in the view. |
delay | number | 0 | Delay in scroll before new data is loaded. |
resizeDelay | number | 10 | Delay after window's resize finishes. |
lazy | boolean | false | Defines if data is loaded and interacted with in lazy manner. |
disabled | boolean | false | If disabled, the VirtualScroller feature is eliminated and the content is displayed directly. |
loaderDisabled | boolean | false | Used to implement a custom loader instead of using the loader feature in the VirtualScroller. |
loading | boolean | false | Whether the data is loaded. |
autoSize | boolean | false | Whether to dynamically change the height or width of scrollable container. |
showSpacer | boolean | true | Used to implement a custom spacer instead of using the spacer feature in the VirtualScroller. |
showLoader | boolean | false | Whether to show loader. |
loadingTemplate | any | null | The template of loader. |
loaderIconTemplate | any | null | The template of loader's icon. |
itemTemplate | any | null | The template of item. |
contentTemplate | any | null | The template of item's wrapper element. |
Name | Parameters | Description |
---|---|---|
onScroll | event: Browser event | Callback to invoke when scroll position changes. |
onScrollIndexChange | event.first: First index of the new data range to be loaded. event.last: Last index of the new data range to be loaded. | Callback to invoke when scroll position and item's range in view changes. |
onLazyLoad | event.first: First index of the new data range to be loaded. event.last: Last index of the new data range to be loaded. | Callback to invoke in lazy mode to load new data. |
Name | Parameters | Description |
---|---|---|
scrollTo | left: Left position of scroll. top: Top position of scroll behavior: Behavior of scroll, valid values are 'auto' and 'smooth' | Scroll to move to a specific position. |
scrollToIndex | index: Index of item according to orientation mode. behavior: Behavior of scroll, valid values are 'auto' and 'smooth' | Scroll to move to a specific item. |
scrollInView | index: Index of item according to orientation mode. to: Defines the location of the item in view, valid values are 'to-start' and 'to-end'. behavior: Behavior of scroll, valid values are 'auto' and 'smooth' | It is used to move the specified index into the view. It is a method that will usually be needed when keyboard support is added to the virtualScroller component. |
getRenderedRange | - | Returns the range of items added to the DOM. |
getElementRef | - | Returns the reference of virtualScroller's container. |
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-virtualscroller | Container element. |
p-virtualscroller-content | Content element. |
p-virtualscroller-loader | Loader element. |
This section is under development. After the necessary tests and improvements are made, it will be shared with the users as soon as possible.
None.
Built-in component themes created by the PrimeReact Theme Designer.
Premium themes are only available exclusively for PrimeReact Theme Designer subscribers and therefore not included in PrimeReact core.
Beautifully crafted premium create-react-app application templates by the PrimeTek design team.