react-tiny-virtual-list
A tiny but mighty 3kb list virtualization library, with zero dependencies 💪 Supports variable heights/widths, sticky items, scrolling to index, and more!
Top Related Projects
React components for efficiently rendering large lists and tabular data
React components for efficiently rendering large lists and tabular data
The most powerful virtual list component for React
🤖 Headless UI for Virtualizing Large Element Lists in JS/TS, React, Solid, Vue and Svelte
Drag and Drop for React
Quick Overview
React Tiny Virtual List is a lightweight and efficient React component for rendering large lists and tabular data. It implements virtualization techniques to render only the visible items, significantly improving performance for long lists.
Pros
- Highly performant, capable of rendering thousands of items smoothly
- Tiny bundle size (< 5kb gzipped) for minimal impact on application load times
- Supports both fixed and variable height items
- Easy to integrate with existing React projects
Cons
- Limited built-in features compared to more comprehensive virtualization libraries
- May require additional configuration for complex use cases
- Documentation could be more extensive
- Not actively maintained (last update was in 2019)
Code Examples
- Basic usage with fixed height items:
import React from 'react';
import VirtualList from 'react-tiny-virtual-list';
const MyList = () => (
<VirtualList
width='100%'
height={400}
itemCount={1000}
itemSize={50}
renderItem={({ index, style }) => (
<div key={index} style={style}>
Item {index}
</div>
)}
/>
);
- Using variable height items:
import React from 'react';
import VirtualList from 'react-tiny-virtual-list';
const MyVariableList = () => (
<VirtualList
width='100%'
height={400}
itemCount={1000}
itemSize={index => (index % 2 === 0 ? 50 : 75)}
renderItem={({ index, style }) => (
<div key={index} style={style}>
Item {index} (Height: {index % 2 === 0 ? 50 : 75}px)
</div>
)}
/>
);
- Implementing a scrollToIndex function:
import React, { useRef } from 'react';
import VirtualList from 'react-tiny-virtual-list';
const ScrollableList = () => {
const listRef = useRef();
const scrollToIndex = (index) => {
listRef.current.scrollToIndex(index);
};
return (
<>
<button onClick={() => scrollToIndex(50)}>Scroll to index 50</button>
<VirtualList
ref={listRef}
width='100%'
height={400}
itemCount={1000}
itemSize={50}
renderItem={({ index, style }) => (
<div key={index} style={style}>
Item {index}
</div>
)}
/>
</>
);
};
Getting Started
-
Install the package:
npm install react-tiny-virtual-list
-
Import and use in your React component:
import React from 'react'; import VirtualList from 'react-tiny-virtual-list'; const MyComponent = () => ( <VirtualList width='100%' height={400} itemCount={1000} itemSize={50} renderItem={({ index, style }) => ( <div key={index} style={style}>Item {index}</div> )} /> ); export default MyComponent;
Competitor Comparisons
React components for efficiently rendering large lists and tabular data
Pros of react-window
- More lightweight and performant, especially for large lists
- Supports both fixed and variable size lists/grids
- Better documentation and examples
Cons of react-window
- Less flexible for complex layouts
- Requires more setup and configuration
- Limited built-in features compared to react-tiny-virtual-list
Code Comparison
react-window:
import { FixedSizeList } from 'react-window';
const Example = () => (
<FixedSizeList
height={400}
itemCount={1000}
itemSize={35}
width={300}
>
{({ index, style }) => <div style={style}>Row {index}</div>}
</FixedSizeList>
);
react-tiny-virtual-list:
import VirtualList from 'react-tiny-virtual-list';
const Example = () => (
<VirtualList
width={300}
height={400}
itemCount={1000}
itemSize={35}
renderItem={({ index, style }) => (
<div style={style}>Row {index}</div>
)}
/>
);
Both libraries aim to provide efficient rendering for large lists, but react-window offers more flexibility and performance optimizations at the cost of slightly more complex setup. react-tiny-virtual-list provides a simpler API but may not be as performant for extremely large datasets. The code examples show similar usage, with react-window requiring separate imports for different list types.
React components for efficiently rendering large lists and tabular data
Pros of react-virtualized
- More comprehensive feature set, including support for various layouts (Grid, List, Table, etc.)
- Better performance for large datasets due to advanced optimization techniques
- Extensive documentation and community support
Cons of react-virtualized
- Larger bundle size, which may impact initial load times
- Steeper learning curve due to its complexity and numerous configuration options
- More frequent updates and potential breaking changes
Code Comparison
react-tiny-virtual-list:
import VirtualList from 'react-tiny-virtual-list';
<VirtualList
width='100%'
height={600}
itemCount={1000}
itemSize={50}
renderItem={({ index, style }) => (
<div key={index} style={style}>Row {index}</div>
)}
/>
react-virtualized:
import { List } from 'react-virtualized';
<List
width={300}
height={300}
rowCount={1000}
rowHeight={50}
rowRenderer={({ key, index, style }) => (
<div key={key} style={style}>Row {index}</div>
)}
/>
Both libraries provide efficient rendering of large lists, but react-virtualized offers more flexibility and features at the cost of increased complexity. react-tiny-virtual-list is simpler and lighter, making it suitable for projects with basic virtualization needs.
The most powerful virtual list component for React
Pros of react-virtuoso
- More feature-rich, including support for grouping, sticky headers, and footer
- Better TypeScript support and type definitions
- More active development and maintenance
Cons of react-virtuoso
- Slightly larger bundle size
- May have a steeper learning curve due to more features and options
- Potentially higher memory usage for complex lists
Code comparison
react-tiny-virtual-list:
import VirtualList from 'react-tiny-virtual-list';
<VirtualList
width='100%'
height={600}
itemCount={items.length}
itemSize={50}
renderItem={({index, style}) => (
<div key={index} style={style}>{items[index]}</div>
)}
/>
react-virtuoso:
import { Virtuoso } from 'react-virtuoso';
<Virtuoso
style={{ height: '400px' }}
totalCount={items.length}
itemContent={index => <div>{items[index]}</div>}
/>
Both libraries provide efficient rendering of large lists, but react-virtuoso offers more advanced features and better TypeScript support. react-tiny-virtual-list is simpler and may be easier to integrate for basic use cases. The code examples show that react-virtuoso has a slightly more concise API, while react-tiny-virtual-list requires more explicit configuration.
🤖 Headless UI for Virtualizing Large Element Lists in JS/TS, React, Solid, Vue and Svelte
Pros of TanStack/virtual
- Framework-agnostic, supporting React, Vue, Solid, and more
- More advanced features like multi-dimensional virtualization and dynamic item sizes
- Active development and larger community support
Cons of TanStack/virtual
- Steeper learning curve due to more complex API
- Potentially higher bundle size for simpler use cases
- May require additional setup for basic virtualization scenarios
Code Comparison
react-tiny-virtual-list:
import VirtualList from 'react-tiny-virtual-list';
<VirtualList
width='100%'
height={600}
itemCount={1000}
itemSize={50}
renderItem={({ index, style }) => (
<div style={style}>Item {index}</div>
)}
/>
TanStack/virtual:
import { useVirtualizer } from '@tanstack/react-virtual';
const virtualizer = useVirtualizer({
count: 1000,
getScrollElement: () => parentRef.current,
estimateSize: () => 50,
});
{virtualizer.getVirtualItems().map((virtualItem) => (
<div key={virtualItem.key} style={{
height: `${virtualItem.size}px`,
transform: `translateY(${virtualItem.start}px)`
}}>
Item {virtualItem.index}
</div>
))}
Both libraries provide efficient virtualization for large lists, but TanStack/virtual offers more flexibility and advanced features at the cost of increased complexity.
Drag and Drop for React
Pros of react-dnd
- Comprehensive drag-and-drop functionality with support for complex interactions
- Highly customizable with a flexible API for various use cases
- Large community and extensive documentation
Cons of react-dnd
- Steeper learning curve due to its complexity and abstraction layers
- Potentially heavier bundle size for simpler drag-and-drop needs
- May require more setup and configuration for basic use cases
Code Comparison
react-dnd:
import { useDrag, useDrop } from 'react-dnd';
function DraggableItem({ id, text }) {
const [{ isDragging }, drag] = useDrag(() => ({
type: 'ITEM',
item: { id },
collect: (monitor) => ({
isDragging: !!monitor.isDragging(),
}),
}));
return <div ref={drag}>{text}</div>;
}
react-tiny-virtual-list:
import VirtualList from 'react-tiny-virtual-list';
function VirtualizedList({ items }) {
return (
<VirtualList
width="100%"
height={600}
itemCount={items.length}
itemSize={50}
renderItem={({ index, style }) => (
<div style={style}>{items[index]}</div>
)}
/>
);
}
Note: The code comparison highlights the different focus of these libraries. react-dnd is centered around drag-and-drop functionality, while react-tiny-virtual-list is focused on efficient rendering of large lists.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
react-tiny-virtual-list
A tiny but mighty list virtualization library, with zero dependencies ðª
- Tiny & dependency free â Only 3kb gzipped
- Render millions of items, without breaking a sweat
- Scroll to index or set the initial scroll offset
- Supports fixed or variable heights/widths
- Vertical or Horizontal lists
Check out the demo for some examples, or take it for a test drive right away in Code Sandbox.
Getting Started
Using npm:
npm install react-tiny-virtual-list --save
ES6, CommonJS, and UMD builds are available with each distribution. For example:
import VirtualList from 'react-tiny-virtual-list';
You can also use a global-friendly UMD build:
<script src="react-tiny-virtual-list/umd/react-tiny-virtual-list.js"></script>
<script>
var VirtualList = window.VirtualList;
...
</script>
Example usage
import React from 'react';
import {render} from 'react-dom';
import VirtualList from 'react-tiny-virtual-list';
const data = ['A', 'B', 'C', 'D', 'E', 'F', ...];
render(
<VirtualList
width='100%'
height={600}
itemCount={data.length}
itemSize={50} // Also supports variable heights (array or function getter)
renderItem={({index, style}) =>
<div key={index} style={style}> // The style property contains the item's absolute position
Letter: {data[index]}, Row: #{index}
</div>
}
/>,
document.getElementById('root')
);
Prop Types
Property | Type | Required? | Description |
---|---|---|---|
width | Number | String* | â | Width of List. This property will determine the number of rendered items when scrollDirection is 'horizontal' . |
height | Number | String* | â | Height of List. This property will determine the number of rendered items when scrollDirection is 'vertical' . |
itemCount | Number | â | The number of items you want to render |
renderItem | Function | â | Responsible for rendering an item given it's index: ({index: number, style: Object}): React.PropTypes.node . The returned element must handle key and style. |
itemSize | â | Either 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 | |
scrollDirection | String | Whether the list should scroll vertically or horizontally. One of 'vertical' (default) or 'horizontal' . | |
scrollOffset | Number | Can be used to control the scroll offset; Also useful for setting an initial scroll offset | |
scrollToIndex | Number | Item index to scroll to (by forcefully scrolling if necessary) x | |
scrollToAlignment | String | Used in combination with scrollToIndex , 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 specified scrollToIndex item is fully visible. | |
stickyIndices | Number[] | An array of indexes (eg. [0, 10, 25, 30] ) to make certain items in the list sticky (position: sticky ) | |
overscanCount | Number | Number of extra buffer items to render above/below the visible items. Tweaking this can help reduce scroll flickering on certain browsers/devices. | |
estimatedItemSize | Number | Used 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. | |
onItemsRendered | Function | Callback invoked with information about the slice of rows/columns that were just rendered. It has the following signature: ({startIndex: number, stopIndex: number}) . | |
onScroll | Function | Callback 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 when scrollDirection
is 'vertical'
. Similarly, Height may only be a string if scrollDirection
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 a number
, this isn't an issue, as it can compare before and after values and automatically call recomputeSizes
internally.
However, if you're passing a function to itemSize
, that type of comparison is error prone. In that event, you'll need to call recomputeSizes
manually to inform the VirtualList
that the size of its items has changed.
Common Issues with PureComponent
react-tiny-virtual-list
uses PureComponent, 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 calling forceUpdate on it or by passing it an extra prop that will change every time your data changes.
Reporting Issues
Found an issue? Please report it along with any relevant details to reproduce it. If you can, please provide a live demo replicating the issue you're describing. You can fork 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 from react-virtualized, and is meant as a bare-minimum replacement for the List 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 out react-virtualized instead, it's a fantastic library â¤ï¸
License
react-tiny-virtual-list is available under the MIT License.
Top Related Projects
React components for efficiently rendering large lists and tabular data
React components for efficiently rendering large lists and tabular data
The most powerful virtual list component for React
🤖 Headless UI for Virtualizing Large Element Lists in JS/TS, React, Solid, Vue and Svelte
Drag and Drop for React
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot