VirtualizedMessageListContext
VirtualizedMessageListContext gives the components inside a VirtualizedMessageList one function: scroll to the latest message. Read it with useVirtualizedMessageListContext(). Message list UI explains the list itself.
Best practices
- Call
useVirtualizedMessageListContext()only in a component thatVirtualizedMessageListrenders. It throws anywhere else, including insideMessageList. - Scroll with
scrollToBottom()rather than through the DOM. The list renders only the messages near the visible area, so most messages have no DOM node to scroll to.
Basic usage
The NewMessageNotification override renders inside the list. This one is a button that takes the user to the latest message:
import {
useVirtualizedMessageListContext,
VirtualizedMessageList,
WithComponents,
} from "stream-chat-react";
import type { NewMessageNotificationProps } from "stream-chat-react";
const JumpToLatest = ({
newMessageCount,
showNotification,
}: NewMessageNotificationProps) => {
const { scrollToBottom } = useVirtualizedMessageListContext();
if (!showNotification) return null;
return (
<button className="jump-to-latest" onClick={scrollToBottom} type="button">
{newMessageCount ? `${newMessageCount} new messages` : "New messages"}
</button>
);
};
const overrides = { NewMessageNotification: JumpToLatest };
export const LivestreamMessages = () => (
<WithComponents overrides={overrides}>
<VirtualizedMessageList />
</WithComponents>
);Values
| Value | Type | Description |
|---|---|---|
scrollToBottom |
() => void |
Scrolls to the latest message and hides the new message notification. While the list shows an older interval, it jumps to the latest messages instead. |
Related hooks
useVirtualizedMessageListContext()returns the value above, and throws outsideVirtualizedMessageList.useMessageListContext()is the counterpart forMessageList.