Imperative API
DataMethods
Section titled “DataMethods”Methods for manipulating the data in the list.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type | Description |
|---|---|---|
Data | any | The type of the data items in the list. |
Context | any | The type of the context passed to the list. |
Properties
Section titled “Properties”append()
Section titled “append()”append: (
data,scrollToBottom?) =>void
Appends additional items to the existing data in the list, while optionally updating the scroll position. See the AutoscrollToBottom type for more information.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
data | Data[] | The data to append. |
scrollToBottom? | AutoscrollToBottom<Data, Context> | Specifies the behavior when the list is scrolled to the bottom. You can pass a boolean, a ScrollBehavior, or a function that returns a ScrollBehavior. |
Returns
Section titled “Returns”void
batch()
Section titled “batch()”batch: (
callback,scrollToBottom?) =>void
Batches the data operations in the provided callback in a single render cycle.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
callback | () => void | The callback that performs the data operations. |
scrollToBottom? | AutoscrollToBottom<Data, Context> | Specifies the behavior when the list is scrolled to the bottom. You can pass a boolean, a ScrollBehavior, or a function that returns a ScrollBehavior. |
Returns
Section titled “Returns”void
deleteRange()
Section titled “deleteRange()”deleteRange: (
offset,count) =>void
Deletes a range of items from the list data.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
offset | number | The index of the first item to delete. |
count | number | The number of items to delete. |
Returns
Section titled “Returns”void
find()
Section titled “find()”find: (
predicate) =>Data|undefined
Finds the first item that matches the predicate. If no elements satisfy the testing function, undefined is returned.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
predicate | (item, index, data) => boolean | A function that returns true for the item being searched for. |
Returns
Section titled “Returns”Data | undefined
findAndDelete()
Section titled “findAndDelete()”findAndDelete: (
predicate) =>void
Deletes items from the list data that match the predicate.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
predicate | (item, index) => boolean | A function that returns true for items that should be deleted. |
Returns
Section titled “Returns”void
findIndex()
Section titled “findIndex()”findIndex: (
predicate) =>number
Finds the index of the first item that matches the predicate. If no elements satisfy the testing function, -1 is returned.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
predicate | (item, index, data) => boolean | A function that returns true for the item being searched for. |
Returns
Section titled “Returns”number
get: () =>
Data[]
Gets a shallow copy of the current data in the list.
Returns
Section titled “Returns”Data[]
getCurrentlyRendered()
Section titled “getCurrentlyRendered()”getCurrentlyRendered: () =>
Data[]
Gets the currently rendered data items.
Returns
Section titled “Returns”Data[]
insert()
Section titled “insert()”insert: (
data,offset,scrollToBottom?) =>void
Inserts the provided data at the specified offset, optionally updating the scroll position.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
data | Data[] | The data to insert. |
offset | number | The index that the first item in the data will be inserted at. e.g. [1,2,3] with offset 10 will insert the data at index 10, 11, and 12 and shift the rest of the existing data. |
scrollToBottom? | AutoscrollToBottom<Data, Context> | Specifies the behavior when the list is scrolled to the bottom. You can pass a boolean, a ScrollBehavior, or a function that returns a ScrollBehavior. |
Returns
Section titled “Returns”void
map: (
callbackfn,autoscrollToBottomBehavior?) =>void
Updates the data in the list by applying a mapping function to each item. Optionally, you can specify a scroll behavior if the state change displaces the list (for example, if the item size increases).
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
callbackfn | (data, index) => Data | A function that maps the data items. |
autoscrollToBottomBehavior? | ScrollBehavior | { location: () => ItemLocation | null | undefined; } | Specifies the behavior to use to scroll to the bottom if necessary. |
Returns
Section titled “Returns”void
mapWithAnchor()
Section titled “mapWithAnchor()”mapWithAnchor: (
callbackfn,anchorItemIndex) =>void
Updates the data in the list by applying a mapping function to each item, while keeping the specified item anchored in the viewport. This is useful when the size of some items change, and you want to avoid displacing the scroll position.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
callbackfn | (data, index) => Data | A function that maps the data items. |
anchorItemIndex | number | the index of the item to keep anchored in the viewport. |
Returns
Section titled “Returns”void
prepend()
Section titled “prepend()”prepend: (
data) =>void
Prepends additional items to the existing data in the list, while preserving the scroll position.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
data | Data[] | The data to prepend. |
Returns
Section titled “Returns”void
removeFromStart()
Section titled “removeFromStart()”removeFromStart: (
count) =>void
Removes the specified amount of items from the start of the list.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
count | number | The number of items to remove. |
Returns
Section titled “Returns”void
replace()
Section titled “replace()”replace: (
data,options?) =>void
Completely replaces the data in the list with the new data.
Optionally, you can specify an initial scroll location after the data has been replaced.
See the ItemLocation type for more information.
Setting the purgeItemSizes flag to true will clear the item size cache and force the list to remeasure the items.
The suppressItemMeasure flag will prevent the list from measuring the items after the data has been replaced.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
data | Data[] | The data to replace. |
options? | { initialLocation?: ItemLocation; purgeItemSizes?: boolean; suppressItemMeasure?: boolean; } | The location to scroll to after the data has been replaced and whether to purge the item sizes. |
options.initialLocation? | ItemLocation | - |
options.purgeItemSizes? | boolean | - |
options.suppressItemMeasure? | boolean | - |
Returns
Section titled “Returns”void
VirtuosoMessageListMethods
Section titled “VirtuosoMessageListMethods”The imperative API of the message list component. You can access it with a ref, or by using the `useVirtuosoMethods()` hook from a child component.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type | Description |
|---|---|---|
Data | any | The type of the data items in the list. Specifying this type gives you correct typing in the data methods. |
Context | any | The type of the context passed to the list. Specifying this type gives you correct typing in the data methods. |
Properties
Section titled “Properties”cancelSmoothScroll()
Section titled “cancelSmoothScroll()”cancelSmoothScroll: () =>
void
Cancels the current smooth scroll operation, if any.
Returns
Section titled “Returns”void
DataMethods<Data, Context>
A set of methods to manipulate the data in the list.
getScrollLocation()
Section titled “getScrollLocation()”getScrollLocation: () =>
ListScrollLocation
Retrieves the current scroll location
Returns
Section titled “Returns”height()
Section titled “height()”height: (
item) =>number
Gets the known height of the item.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
item | Data |
Returns
Section titled “Returns”number
scrollerElement()
Section titled “scrollerElement()”scrollerElement: () =>
HTMLDivElement|null
Lets you obtain a reference to the component’s scroller DOM element.
Returns
Section titled “Returns”HTMLDivElement | null
scrollIntoView()
Section titled “scrollIntoView()”scrollIntoView: (
location) =>void
Scrolls the specified item into view if necessary. See ItemLocation for possible location details. Passing a number scrolls to the item at the specified index.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
location | ItemLocation |
Returns
Section titled “Returns”void
scrollToItem()
Section titled “scrollToItem()”scrollToItem: (
location) =>void
Scrolls the list to the specified item. See ItemLocation for possible location details. Passing a number scrolls to the item at the specified index aligned to the top.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
location | ItemLocation |
Returns
Section titled “Returns”void