Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A tiny but mighty 3kb list virtualization library, with zero dependencies 💪 Supports variable heights/widths, sticky items, scrolling to index, and more!

License

NotificationsYou must be signed in to change notification settings

clauderic/react-tiny-virtual-list

Repository files navigation

react-tiny-virtual-list

A tiny but mighty list virtualization library, with zero dependencies 💪

npm versionnpm downloadsBuild StatuscodecovtypescriptlicenseGitter

  • Tiny & dependency free – Only 3kb gzipped
  • Render millions of items, without breaking a sweat
  • Scroll to index orset the initial scroll offset
  • Supports fixed orvariable heights/widths
  • Vertical orHorizontal lists

Check out thedemo for some examples, or take it for a test drive right away inCode Sandbox.

Getting Started

Usingnpm:

npm install react-tiny-virtual-list --save

ES6, CommonJS, and UMD builds are available with each distribution. For example:

importVirtualListfrom'react-tiny-virtual-list';

You can also use a global-friendly UMD build:

<scriptsrc="react-tiny-virtual-list/umd/react-tiny-virtual-list.js"></script><script>varVirtualList=window.VirtualList;...</script>

Example usage

importReactfrom'react';import{render}from'react-dom';importVirtualListfrom'react-tiny-virtual-list';constdata=['A','B','C','D','E','F', ...];render(<VirtualListwidth='100%'height={600}itemCount={data.length}itemSize={50}// Also supports variable heights (array or function getter)renderItem={({index, style})=><divkey={index}style={style}> // The style property contains the item's absolute position        Letter:{data[index]}, Row: #{index}</div>}/>,document.getElementById('root'));

Prop Types

PropertyTypeRequired?Description
widthNumber | String*Width of List. This property will determine the number of rendered items when scrollDirection is'horizontal'.
heightNumber | String*Height of List. This property will determine the number of rendered items when scrollDirection is'vertical'.
itemCountNumberThe number of items you want to render
renderItemFunctionResponsible for rendering an item given it's index:({index: number, style: Object}): React.PropTypes.node. The returned element must handle key and style.
itemSizeEither a fixed height/width (depending on the scrollDirection), an array containing the heights of all the items in your list, or a function that returns the height of an item given its index:(index: number): number
scrollDirectionStringWhether the list should scroll vertically or horizontally. One of'vertical' (default) or'horizontal'.
scrollOffsetNumberCan be used to control the scroll offset; Also useful for setting an initial scroll offset
scrollToIndexNumberItem index to scroll to (by forcefully scrolling if necessary) x
scrollToAlignmentStringUsed in combination withscrollToIndex, this prop controls the alignment of the scrolled to item. One of:'start','center','end' or'auto'. Use'start' to always align items to the top of the container and'end' to align them bottom. Use'center' to align them in the middle of the container.'auto' scrolls the least amount possible to ensure that the specifiedscrollToIndex item is fully visible.
stickyIndicesNumber[]An array of indexes (eg.[0, 10, 25, 30]) to make certain items in the list sticky (position: sticky)
overscanCountNumberNumber of extra buffer items to render above/below the visible items. Tweaking this can help reduce scroll flickering on certain browsers/devices.
estimatedItemSizeNumberUsed to estimate the total size of the list before all of its items have actually been measured. The estimated total height is progressively adjusted as items are rendered.
onItemsRenderedFunctionCallback invoked with information about the slice of rows/columns that were just rendered. It has the following signature:({startIndex: number, stopIndex: number}).
onScrollFunctionCallback invoked whenever the scroll offset changes within the inner scrollable region. It has the following signature:(scrollTop: number, event: React.UIEvent<HTMLDivElement>).

* Width may only be a string whenscrollDirection is'vertical'. Similarly, Height may only be a string ifscrollDirection is'horizontal'

Public Methods

recomputeSizes (index: number)

This method force recomputes the item sizes after the specified index (these are normally cached).

VirtualList has no way of knowing when its underlying data has changed, since it only receives a itemSize property. If the itemSize is anumber, this isn't an issue, as it can compare before and after values and automatically callrecomputeSizes internally.However, if you're passing a function toitemSize, that type of comparison is error prone. In that event, you'll need to callrecomputeSizes manually to inform theVirtualList that the size of its items has changed.

Common Issues with PureComponent

react-tiny-virtual-list usesPureComponent, so it only updates when it's props change. Therefore, if only the order of your data changes (eg['a','b','c'] =>['d','e','f']),react-tiny-virtual-list has no way to know your data has changed and that it needs to re-render.

You can force it to re-render by callingforceUpdate on it or by passing it an extra prop that will change every time your data changes.

Reporting Issues

Found an issue? Pleasereport it along with any relevant details to reproduce it. If you can, please provide a live demo replicating the issue you're describing. You canfork this Code Sandbox as a starting point.

Contributions

Feature requests / pull requests are welcome, though please take a moment to make sure your contributions fits within the scope of the project.Learn how to contribute

Acknowledgments

This library draws inspiration fromreact-virtualized, and is meant as a bare-minimum replacement for theList component. If you're looking for a tiny, lightweight and dependency-free list virtualization library that supports variable heights, you're in the right place! If you're looking for something that supports more use-cases, I highly encourage you to check outreact-virtualized instead, it's a fantastic library ❤️

License

react-tiny-virtual-list is available under the MIT License.

About

A tiny but mighty 3kb list virtualization library, with zero dependencies 💪 Supports variable heights/widths, sticky items, scrolling to index, and more!

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors7


[8]ページ先頭

©2009-2025 Movatter.jp