VirtuosoGrid
VirtuosoGrid
Section titled “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 Parameters
Section titled “Type Parameters”| Type Parameter | Default type | Description |
|---|---|---|
ItemData | any | The type of data items in the grid |
Context | any | The type of additional context passed to callbacks |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
props | VirtuosoGridProps<ItemData, Context> & object | VirtuosoGridProps |
Returns
Section titled “Returns”ReactElement
Example
Section titled “Example”tsx- VirtuosoGridProps for available props
- VirtuosoGridHandle for imperative methods
- GridComponents for customizing grid elements
VirtuosoGridProps
Section titled “VirtuosoGridProps”The props for the VirtuosoGrid component.
- VirtuosoGrid for the component
- VirtuosoGridHandle for imperative methods
Extends
Section titled “Extends”Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type | Description |
|---|---|---|
Data | - | The type of data items in the grid |
Context | unknown | The type of additional context passed to callbacks |
Properties
Section titled “Properties”atBottomStateChange()?
Section titled “atBottomStateChange()?”
optionalatBottomStateChange: (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.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
atBottom | boolean |
Returns
Section titled “Returns”void
atTopStateChange()?
Section titled “atTopStateChange()?”
optionalatTopStateChange: (atTop) =>void
Called with true / false when the list has reached the top / gets scrolled down.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
atTop | boolean |
Returns
Section titled “Returns”void
components?: GridComponents<Context>
Section titled “components?: GridComponents<Context>”Use the components property for advanced customization of the elements rendered by the list.
computeItemKey?: GridComputeItemKey<Data, Context>
Section titled “computeItemKey?: GridComputeItemKey<Data, Context>”If specified, the component will use the function to generate the key property for each list item.
context?: Context
Section titled “context?: Context”Additional context available in the custom components and content callbacks
customScrollParent?: HTMLElement
Section titled “customScrollParent?: HTMLElement”Pass a reference to a scrollable parent element, so that the grid won’t wrap in its own.
data?: readonly Data[]
Section titled “data?: readonly Data[]”The data items to be rendered. If data is set, the total count will be inferred from the length of the array.
endReached()?
Section titled “endReached()?”
optionalendReached: (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.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
index | number |
Returns
Section titled “Returns”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.
initialItemCount?: number
Section titled “initialItemCount?: number”Use for server-side rendering - if set, the list will render the specified amount of items regardless of the container / item size.
initialTopMostItemIndex?: GridIndexLocation
Section titled “initialTopMostItemIndex?: GridIndexLocation”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.
isScrolling()?
Section titled “isScrolling()?”
optionalisScrolling: (isScrolling) =>void
Called when the list starts/stops scrolling.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
isScrolling | boolean |
Returns
Section titled “Returns”void
itemClassName?: string
Section titled “itemClassName?: string”Sets the grid items’ className
itemContent?: GridItemContent<Data, Context>
Section titled “itemContent?: GridItemContent<Data, Context>”Set the callback to specify the contents of the item.
listClassName?: string
Section titled “listClassName?: string”Sets the className for the list DOM element
logLevel?: LogLevel
Section titled “logLevel?: LogLevel”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).
rangeChanged()?
Section titled “rangeChanged()?”
optionalrangeChanged: (range) =>void
Called with the new set of items each time the list items are rendered due to scrolling.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
range | ListRange |
Returns
Section titled “Returns”void
readyStateChanged()?
Section titled “readyStateChanged()?”
optionalreadyStateChanged: (ready) =>void
invoked with true after the grid has done the initial render and the items have been measured.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
ready | boolean |
Returns
Section titled “Returns”void
restoreStateFrom?: GridStateSnapshot | null
Section titled “restoreStateFrom?: GridStateSnapshot | null”Pass a state obtained from the stateChanged callback to restore the grid state.
This includes scroll position and item measurements.
scrollerRef()?
Section titled “scrollerRef()?”
optionalscrollerRef: (ref) =>any
Provides access to the root DOM element
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
ref | HTMLElement | null |
Returns
Section titled “Returns”any
scrollSeekConfiguration?: false | ScrollSeekConfiguration
Section titled “scrollSeekConfiguration?: false | ScrollSeekConfiguration”Use to display placeholders if the user scrolls fast through the list.
Set components.ScrollSeekPlaceholder to change the placeholder content.
startReached()?
Section titled “startReached()?”
optionalstartReached: (index) =>void
Called when the user scrolls to the start of the list.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
index | number |
Returns
Section titled “Returns”void
stateChanged()?
Section titled “stateChanged()?”
optionalstateChanged: (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.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
state | GridStateSnapshot |
Returns
Section titled “Returns”void
totalCount?: number
Section titled “totalCount?: number”The total amount of items to be rendered.
useWindowScroll?: boolean
Section titled “useWindowScroll?: boolean”Uses the document scroller rather than wrapping the grid in its own.
VirtuosoGridHandle
Section titled “VirtuosoGridHandle”Exposes the VirtuosoGrid component methods for imperative control. Access via ref on the VirtuosoGrid component.
- VirtuosoGrid for the component
- VirtuosoGridProps for available props
Methods
Section titled “Methods”scrollBy()
Section titled “scrollBy()”scrollBy(
location):void
Scrolls the component by the specified amount.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
location | ScrollToOptions | The scroll offset options |
Returns
Section titled “Returns”void
scrollTo()
Section titled “scrollTo()”scrollTo(
location):void
Scrolls the component to the specified position.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
location | ScrollToOptions | The scroll position options |
Returns
Section titled “Returns”void
scrollToIndex()
Section titled “scrollToIndex()”scrollToIndex(
location):void
Scrolls the component to the specified item index.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
location | GridIndexLocation | The item index or location with alignment options |
Returns
Section titled “Returns”void
GridComponents
Section titled “GridComponents”Customize the VirtuosoGrid rendering by passing a set of custom components.
Example
Section titled “Example”tsxVirtuosoGridProps.components for usage in VirtuosoGrid
Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type | Description |
|---|---|---|
Context | any | The type of additional context passed to components |
Properties
Section titled “Properties”Footer?: ComponentType<ContextProp<Context>>
Section titled “Footer?: ComponentType<ContextProp<Context>>”Set to render a component at the bottom of the list.
Header?: ComponentType<ContextProp<Context>>
Section titled “Header?: ComponentType<ContextProp<Context>>”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.
ScrollSeekPlaceholder?: ComponentType<GridScrollSeekPlaceholderProps & ContextProp<Context>>
Section titled “ScrollSeekPlaceholder?: ComponentType<GridScrollSeekPlaceholderProps & ContextProp<Context>>”Set to render an item placeholder when the user scrolls fast.
See the scrollSeekConfiguration property for more details.
ElementDimensions
Section titled “ElementDimensions”Dimensions of an element in pixels.
Properties
Section titled “Properties”height
Section titled “height”number
Height in pixels
number
Width in pixels
Gap between grid items in pixels.
Properties
Section titled “Properties”column
Section titled “column”number
Horizontal gap between columns
number
Vertical gap between rows
GridComputeItemKey
Section titled “GridComputeItemKey”GridComputeItemKey<
Data,Context> = (index,item,context) =>React.Key
Callback type for computing unique keys for grid items.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Description |
|---|---|
Data | The type of the data item |
Context | The type of context passed from the component |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
index | number |
item | Data |
context | Context |
Returns
Section titled “Returns”React.Key
VirtuosoGridProps.computeItemKey for usage
GridIndexLocation
Section titled “GridIndexLocation”GridIndexLocation =
FlatIndexLocationWithAlign|number
GridItem
Section titled “GridItem”Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
Data |
Properties
Section titled “Properties”data?: Data
Section titled “data?: Data”The data associated with this grid item
number
The index of the item in the grid
GridItemContent
Section titled “GridItemContent”GridItemContent<
Data,Context> = (index,data,context) =>React.ReactNode
Callback type for rendering item content in a VirtuosoGrid.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Description |
|---|---|
Data | The type of the data item |
Context | The type of context passed from the component |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
index | number |
data | Data |
context | Context |
Returns
Section titled “Returns”React.ReactNode
Example
Section titled “Example”tsxVirtuosoGridProps.itemContent for usage
GridItemProps
Section titled “GridItemProps”GridItemProps =
Pick<React.ComponentProps<"div">,"children"|"className"|"style"> &React.RefAttributes<HTMLDivElement> &object
Passed to the GridComponents.Item custom component
Type Declaration
Section titled “Type Declaration”data-index
Section titled “data-index”number
GridListProps
Section titled “GridListProps”GridListProps =
Pick<React.ComponentProps<"div">,"children"|"className"|"style"> &React.RefAttributes<HTMLDivElement> &object
Passed to the GridComponents.List custom component
Type Declaration
Section titled “Type Declaration”data-testid
Section titled “data-testid”string
GridRootProps
Section titled “GridRootProps”GridRootProps =
Omit<React.HTMLProps<HTMLDivElement>,"data"|"ref">
GridScrollSeekPlaceholderProps
Section titled “GridScrollSeekPlaceholderProps”Passed to the GridComponents.ScrollSeekPlaceholder custom component
Properties
Section titled “Properties”height
Section titled “height”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
GridStateSnapshot
Section titled “GridStateSnapshot”A snapshot of the VirtuosoGrid state that can be saved and restored. Use this to persist scroll position and layout across page reloads.
- VirtuosoGridProps.restoreStateFrom for restoring state
- VirtuosoGridProps.stateChanged for capturing state
Properties
Section titled “Properties”Gap between items
Item dimensions
scrollTop
Section titled “scrollTop”number
Scroll position in pixels
viewport
Section titled “viewport”Viewport dimensions
VirtuosoGridMockContext
Section titled “VirtuosoGridMockContext”
constVirtuosoGridMockContext:Context<VirtuosoGridMockContextValue|undefined>
React context for mocking VirtuosoGrid component measurements in tests. Wrap your VirtuosoGrid component with this provider to bypass DOM measurements.
Example
Section titled “Example”tsxVirtuosoGridMockContextValue
Section titled “VirtuosoGridMockContextValue”Mock context value for testing VirtuosoGrid components. Provides fixed dimensions to bypass DOM measurements.
Properties
Section titled “Properties”itemHeight
Section titled “itemHeight”number
Fixed height for each grid item in pixels
itemWidth
Section titled “itemWidth”number
Fixed width for each grid item in pixels
viewportHeight
Section titled “viewportHeight”number
Fixed viewport height in pixels
viewportWidth
Section titled “viewportWidth”number
Fixed viewport width in pixels