react-virtual-list allows you to display a large list of fixed-height items, while only rendering the items visible on the screen. This allows a large list to be rendered with much fewer DOM elements.
Some other benefits:
- One dependency (and it's
- Performant - demo page almost always stays over 60fps http://i.imgur.com/CHVCK9x.png
- Keeps your components separate - as a higher-order component
- Gives you control - doesn't force any particular markup, but gives you the necessary styles and data to use.
If you're looking for documentation on version 1, supporting React
~0.13.x, it's here.
> npm install react-virtual-list --save
./lib/VirtualList.js module exports a single, ES5-compatible, CommonJS-accessible, component factory.
Your inner list component uses the
virtual property to render the visible items, and set a style to set the overall list height and padding.
const MyList =virtualitemHeight<ul style=virtualstyle>virtualitems</ul>;
Note: You should set keys on your list items.
Create the virtualized component.
const MyVirtualList = MyList;
Write the JSX for the virtualized component with the necessary properties.
Options are used before the virtualized component can be created. This means that if you need to change an option after the initial render, you will need to recreate the virtualized component.
const options =container: thisrefscontainer // use this scrollable element as a containerinitialState:firstItemIndex: 0 // show first ten itemslastItemIndex: 9 // during initial render;const MyVirtualList = MyList;
||DOM Element||window||Scrollable element that contains the list. Use this if you have a list inside an element with
||object||-||An object with
These properties and any others set on your virtual component, such as
className, will be passed down to the inner component.
||Array||-||Full array of list items. Only the visible subset of these will be rendered.|
||Number||-||Height in pixels of a single item. You must have a CSS rule that sets the height of each list item to this value.|
||Number||0||Number of items that should be rendered before and after the visible viewport. Try using this if you have a complex list that suffers from a bit of lag when scrolling.|
VirtualList allows a second, optional, parameter, named
mapVirtualToProps, which functions similarly to Redux's
mapStateToProps. This function can be provided to change the properties passed to
MyList. Its arguments are:
||Includes all properties passed to
mapVirtualToProps can be found here.
npm test to run the tests using Jest. Not everything is currently tested yet!