2.0.1 • Published 5 months ago

virtual-list-lite v2.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Virtual List

For efficiently rendering large lists data, simple and easy to use.

Installation

npm i virtual-list-lite --save

Usage

VirtualList

import { VirtualList, ScrollDirection } from 'virtual-list-lite'
const virtualList = new VirtualList({
  itemExtent: 64,
  bufferCount: 3,
  countToTheTrailing: 3,
  direction: ScrollDirection.vertical,
})

container.addEventListener('scroll', (e) => {
  const {
    shouldUpdate,
    isReachTheEnd,
    startIndex,
    endIndex,
    paddingLeading,
    paddingTrailing,
    shouldScrollToLeading
  } = virtualList.compute(
    container.offsetHeight,
    e.scrollTop,
    data.length,
    Array.from(container.children),
  );
});
Property NameTypeRequiredDefaultDescription
itemExtentnumbertrue-The size of each item, in order to calculate the size of the virtual list
bufferCountnumbertrue-Number of items of pre-rendered at the leading and trailing
countToTheTrailingnumberfalse0Set the number of items left at the tail to trigger scrolling onReachTheEnd event
directionScrollDirectionfalseScrollDirection.verticalList view scroll direction

VirtualList for React

import { ReactVirtualList } from 'virtual-list-lite'
const App = () => {
  const [userList, setUserList] = useState([])
  const [page, setPage] = useState(0)
  const loadNextPage = () => {
    setPage(page + 1)
  }

  useEffect(() => {
    fetchUserList(page).then(newItems => {
      setUserList([...userList, ...newItems])
    })
  }, [page])

  return (
    <ReactVirtualList
      itemCount={userList.length}
      itemExtent={100}
      bufferCount={3}
      onReachTheEnd={loadNextPage}
      itemBuilder={(index) => (
        <Item data={userList[index]} />
      )}
    />
  )
}
Property NameTypeRequiredDefaultDescription
itemCountnumbertrue-List view item count
itemExtentnumbertrue-The size of each item, in order to calculate the size of the virtual list
bufferCountnumbertrue-Number of items of pre-rendered at the leading and trailing
countToTheTrailingnumberfalse0Set the number of items left at the tail to trigger scrolling onReachTheEnd event
onReachTheEnd() => voidfalse-The event of list view scroll to the end
itemBuilder(index: number) => React.JSXElementtrue-List view item builder function
directionScrollDirectionfalseScrollDirection.verticalList view scroll direction
containerStylesReact.CSSPropertiesfalse-Custom list view container style
scrollerRefReact.RefObjectfalse-If the list view is scrolling by element outside, you should set this property