Collection.md

November 13, 2019 · View on GitHub

Collection

Renders scattered or non-linear data. Unlike Grid, which renders checkerboard data, Collection can render arbitrarily positioned- even overlapping- data.

Note that this component's measuring and layout phase is more expensive than Grid since it can not assume a correlation between a cell's index and position. For this reason it will take significantly longer to initialize than the more linear/checkerboard components.

Prop Types

PropertyTypeRequired?Description
autoHeightBooleanOuter height of Collection is set to "auto". This property should only be used in conjunction with the WindowScroller HOC.
classNameStringOptional custom CSS class name to attach to root Collection element.
cellCountNumberNumber of cells in collection.
cellGroupRendererFunctionResponsible for rendering a group of cells given their indices.: ({ cellSizeAndPositionGetter:Function, indices: Array<number>, cellRenderer: Function }): Array<PropTypes.node>
cellRendererFunctionResponsible for rendering a cell given an row and column index: ({ index: number, isScrolling: boolean, key: string, style: object }): PropTypes.element
cellSizeAndPositionGetterFunctionCallback responsible for returning size and offset/position information for a given cell (index): ({ index: number }): { height: number, width: number, x: number, y: number }
heightNumberHeight of Collection; this property determines the number of visible (vs virtualized) rows.
horizontalOverscanSizeNumberEnables the Collection to horizontally "overscan" its content similar to how Grid does. This can reduce flicker around the edges when a user scrolls quickly. This property defaults to 0;
idStringOptional custom id to attach to root Collection element.
noContentRendererFunctionOptional renderer to be rendered inside the grid when cellCount is 0: (): PropTypes.node
onSectionRenderedFunctionCallback invoked with information about the section of the Collection that was just rendered: ({ indices: Array<number> }): void
onScrollFunctionCallback invoked whenever the scroll offset changes within the inner scrollable region: ({ clientHeight: number, clientWidth: number, scrollHeight: number, scrollLeft: number, scrollTop: number, scrollWidth: number }): void
scrollLeftNumberHorizontal offset
scrollToAlignmentStringControls the alignment of scrolled-to-cells. The default ("auto") scrolls the least amount possible to ensure that the specified cell is fully visible. Use "start" to always align cells to the top/left of the Collection and "end" to align them bottom/right. Use "center" to align specified cell in the middle of container.
scrollToCellNumberCell index to ensure visible (by scrolling if necessary)
scrollTopNumberVertical offset
sectionSizeNumberOptionally override the size of the sections a Collection's cells are split into. This is an advanced option and should only be used for performance tuning purposes.
styleObjectOptional custom inline style to attach to root Collection element.
verticalOverscanSizeNumberEnables the Collection to vertically "overscan" its content similar to how Grid does. This can reduce flicker around the edges when a user scrolls quickly. This property defaults to 0;
widthNumberWidth of Collection; this property determines the number of visible (vs virtualized) columns.

Public Methods

recomputeCellSizesAndPositions

Recomputes cell sizes and positions.

This function should be called if cell sizes or positions have changed but nothing else has. Since Collection only receives cellCount (and not the underlying List or Array) it has no way of detecting when the underlying data changes.

Class names

The Collection component supports the following static class names

PropertyDescription
ReactVirtualized__CollectionMain (outer) element
ReactVirtualizedCollectioninnerScrollContainerInner scrollable area

Examples

Below is a very basic Collection example. It displays an array of objects with fixed row and column sizes. See here for a more full-featured example.

import React from 'react';
import ReactDOM from 'react-dom';
import {Collection} from 'react-virtualized';
import 'react-virtualized/styles.css'; // only needs to be imported once

// Collection data as an array of objects
const list = [
  {name: 'Brian Vaughn', x: 13, y: 34, width: 123, height: 234},
  // And so on...
];

function cellRenderer({index, key, style}) {
  return (
    <div key={key} style={style}>
      {list[index].name}
    </div>
  );
}

function cellSizeAndPositionGetter({index}) {
  const datum = list[index];

  return {
    height: datum.height,
    width: datum.width,
    x: datum.x,
    y: datum.y,
  };
}

// Render your grid
ReactDOM.render(
  <Collection
    cellCount={list.length}
    cellRenderer={cellRenderer}
    cellSizeAndPositionGetter={cellSizeAndPositionGetter}
    height={300}
    width={300}
  />,
  document.getElementById('example'),
);