Skip to content

VirtuosoGrid

VirtuosoGrid<ItemData, Context>(props): ReactElement

A virtualized grid component for efficiently rendering large datasets in a grid/masonry layout. Automatically calculates visible items based on container and item dimensions.

Type ParameterDefault typeDescription
ItemDataanyThe type of data items in the grid
ContextanyThe type of additional context passed to callbacks
ParameterTypeDescription
propsVirtuosoGridProps<ItemData, Context> & objectVirtuosoGridProps

ReactElement

tsx

The props for the VirtuosoGrid component.

Type ParameterDefault typeDescription
Data-The type of data items in the grid
ContextunknownThe type of additional context passed to callbacks

optional atBottomStateChange: (atBottom) => void

Called with true / false when the list has reached the bottom / gets scrolled up. Can be used to load newer items, like tail -f.

ParameterType
atBottomboolean

void


optional atTopStateChange: (atTop) => void

Called with true / false when the list has reached the top / gets scrolled down.

ParameterType
atTopboolean

void


Use the components property for advanced customization of the elements rendered by the list.


If specified, the component will use the function to generate the key property for each list item.


Additional context available in the custom components and content callbacks


Pass a reference to a scrollable parent element, so that the grid won’t wrap in its own.


The data items to be rendered. If data is set, the total count will be inferred from the length of the array.


optional endReached: (index) => void

Gets called when the user scrolls to the end of the list. Receives the last item index as an argument. Can be used to implement endless scrolling.

ParameterType
indexnumber

void


increaseViewportBy?: number | { bottom: number; top: number; }

Section titled “increaseViewportBy?: number | { bottom: number; top: number; }”

The property accepts pixel values.

Set the increaseViewportBy property to artificially increase the viewport size, causing items to be rendered before outside of the viewport. The property causes the component to render more items than the necessary, but can help with slow loading content. Using { top?: number, bottom?: number } lets you set the increase for each end separately.


Use for server-side rendering - if set, the list will render the specified amount of items regardless of the container / item size.


Set to a value between 0 and totalCount - 1 to make the grid start scrolled to that item. Pass in an object to achieve additional effects similar to scrollToIndex.


optional isScrolling: (isScrolling) => void

Called when the list starts/stops scrolling.

ParameterType
isScrollingboolean

void


Sets the grid items’ className


Set the callback to specify the contents of the item.


Sets the className for the list DOM element


set to LogLevel.DEBUG to enable various diagnostics in the console, the most useful being the item measurement reports.

Ensure that you have “all levels” enabled in the browser console too see the messages.


overscan?: number | { main: number; reverse: number; }

Section titled “overscan?: number | { main: number; reverse: number; }”

Set the overscan property to make the component “chunk” the rendering of new items on scroll. The property causes the component to render more items than the necessary, but reduces the re-renders on scroll. Setting { main: number, reverse: number } lets you extend the grid in both the main and the reverse scrollable directions. See the increaseViewportBy property for a similar behavior (equivalent to the overscan in react-window).


optional rangeChanged: (range) => void

Called with the new set of items each time the list items are rendered due to scrolling.

ParameterType
rangeListRange

void


optional readyStateChanged: (ready) => void

invoked with true after the grid has done the initial render and the items have been measured.

ParameterType
readyboolean

void


Pass a state obtained from the stateChanged callback to restore the grid state. This includes scroll position and item measurements.


optional scrollerRef: (ref) => any

Provides access to the root DOM element

ParameterType
refHTMLElement | null

any


Use to display placeholders if the user scrolls fast through the list.

Set components.ScrollSeekPlaceholder to change the placeholder content.


optional startReached: (index) => void

Called when the user scrolls to the start of the list.

ParameterType
indexnumber

void


optional stateChanged: (state) => void

reports when the grid state changes. The reported value can be stored and passed back to restoreStateFrom to restore the grid to the same state.

ParameterType
stateGridStateSnapshot

void


The total amount of items to be rendered.


Uses the document scroller rather than wrapping the grid in its own.


Exposes the VirtuosoGrid component methods for imperative control. Access via ref on the VirtuosoGrid component.

scrollBy(location): void

Scrolls the component by the specified amount.

ParameterTypeDescription
locationScrollToOptionsThe scroll offset options

void


scrollTo(location): void

Scrolls the component to the specified position.

ParameterTypeDescription
locationScrollToOptionsThe scroll position options

void


scrollToIndex(location): void

Scrolls the component to the specified item index.

ParameterTypeDescription
locationGridIndexLocationThe item index or location with alignment options

void


Customize the VirtuosoGrid rendering by passing a set of custom components.

tsx

VirtuosoGridProps.components for usage in VirtuosoGrid

Type ParameterDefault typeDescription
ContextanyThe type of additional context passed to components

Set to render a component at the bottom of the list.


Set to render a component at the top of the list.

The header remains above the top items and does not remain sticky.


Item?: ComponentType<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "className" | "style" | "children"> & RefAttributes<HTMLDivElement> & object & ContextProp<Context>>

Section titled “Item?: ComponentType<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "className" | "style" | "children"> & RefAttributes<HTMLDivElement> & object & ContextProp<Context>>”

Set to customize the item wrapping element. Use only if you would like to render list from elements different than a div.


List?: ComponentType<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "className" | "style" | "children"> & RefAttributes<HTMLDivElement> & object & ContextProp<Context>>

Section titled “List?: ComponentType<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "className" | "style" | "children"> & RefAttributes<HTMLDivElement> & object & ContextProp<Context>>”

Set to customize the items wrapper. Use only if you would like to render list from elements different than a div.


Scroller?: ComponentType<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "style" | "tabIndex" | "children"> & RefAttributes<HTMLDivElement> & object & ContextProp<Context>>

Section titled “Scroller?: ComponentType<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "style" | "tabIndex" | "children"> & RefAttributes<HTMLDivElement> & object & ContextProp<Context>>”

Set to customize the outermost scrollable element. This should not be necessary in general, as the component passes its HTML attribute props to it.


Set to render an item placeholder when the user scrolls fast. See the scrollSeekConfiguration property for more details.


Dimensions of an element in pixels.

number

Height in pixels


number

Width in pixels


Gap between grid items in pixels.

number

Horizontal gap between columns


number

Vertical gap between rows


GridComputeItemKey<Data, Context> = (index, item, context) => React.Key

Callback type for computing unique keys for grid items.

Type ParameterDescription
DataThe type of the data item
ContextThe type of context passed from the component
ParameterType
indexnumber
itemData
contextContext

React.Key

VirtuosoGridProps.computeItemKey for usage


GridIndexLocation = FlatIndexLocationWithAlign | number


Type Parameter
Data

The data associated with this grid item


number

The index of the item in the grid


GridItemContent<Data, Context> = (index, data, context) => React.ReactNode

Callback type for rendering item content in a VirtuosoGrid.

Type ParameterDescription
DataThe type of the data item
ContextThe type of context passed from the component
ParameterType
indexnumber
dataData
contextContext

React.ReactNode

tsx

VirtuosoGridProps.itemContent for usage


GridItemProps = Pick<React.ComponentProps<"div">, "children" | "className" | "style"> & React.RefAttributes<HTMLDivElement> & object

Passed to the GridComponents.Item custom component

number


GridListProps = Pick<React.ComponentProps<"div">, "children" | "className" | "style"> & React.RefAttributes<HTMLDivElement> & object

Passed to the GridComponents.List custom component

string


GridRootProps = Omit<React.HTMLProps<HTMLDivElement>, "data" | "ref">


Passed to the GridComponents.ScrollSeekPlaceholder custom component

number

The height of the placeholder in pixels


number

The index of the item being replaced by the placeholder


number

The width of the placeholder in pixels


A snapshot of the VirtuosoGrid state that can be saved and restored. Use this to persist scroll position and layout across page reloads.

Gap

Gap between items


ElementDimensions

Item dimensions


number

Scroll position in pixels


ElementDimensions

Viewport dimensions


const VirtuosoGridMockContext: Context<VirtuosoGridMockContextValue | undefined>

React context for mocking VirtuosoGrid component measurements in tests. Wrap your VirtuosoGrid component with this provider to bypass DOM measurements.

tsx

Mock context value for testing VirtuosoGrid components. Provides fixed dimensions to bypass DOM measurements.

number

Fixed height for each grid item in pixels


number

Fixed width for each grid item in pixels


number

Fixed viewport height in pixels


number

Fixed viewport width in pixels