Info:
This is release candidate documentation for Stream Chat React SDK v15. For the latest stable version, see the latest version (v14).

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 that VirtualizedMessageList renders. It throws anywhere else, including inside MessageList.
  • 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.
  • useVirtualizedMessageListContext() returns the value above, and throws outside VirtualizedMessageList.
  • useMessageListContext() is the counterpart for MessageList.