Skip to main content

Message UI

How-to Guide for Building a Custom Message#

Using pre-built UI components from the library, the following guide walks you through the process of building a custom Message UI component that looks great and comes packed with functionality. This component must be added to either the Channel or MessageList component via the Message prop.

All of the message UI components in this example have access to the MessageContext, and therefore can call the useMessageContext custom hook. The return value from this hook contains all of the values and functions needed to build a Message UI component.

UI Component Overview#

The custom Message UI component built below imports and uses the following UI components:

  • Attachment - displays the attachments included with a message. See the Attachments section for information on types and props.

  • Avatar - displays the image of the user that sent the message. If the user does not have an image, it will default to the initials of the user's name.

  • MessageOptions - on hover, shows the available options on a message (i.e., react, reply, actions). This component internally renders the MessageActions component, which displays the available actions on a message (ex: edit, flag, pin).

  • MessageRepliesCountButton - displays the number of threaded replies on a message. Clicking the button will call the handleOpenThread function and navigate into a Thread component that lists the replies.

  • MessageStatus - displays message delivery status and shows the users who have read the message.

  • MessageText - formats and renders the message text in markdown using react-markdown. This component also renders QuotedMessage, which provides an additional UI wrapper if the sent message has quoted a previous message.

  • MessageTimestamp - shows the sent time of a message. The default format can be overridden with props.

  • ReactionSelector - allows the connected user to select a reaction on a message. The list of emojis can be customized, but this tutorial uses the default set.

  • SimpleReactionsList - displays a minimal list of the reactions added to a message (alternate option to ReactionsList).

How it Fits Together#

The sample code below assembles the above UI building blocks into a fully featured Message UI component. The UI components allow you to pick the features you wish to employ in your message and orient them in a manner that meets your design specifications. All of the components consume the MessageContext and from pull the necessary data by using the useMessageContext hook.


In advanced use cases, the pre-built, UI components may not provide enough design customization potential. In these situations, we recommend copying the component's functionality and adding your own design and layout.

The Code:#

import React, { useRef } from 'react';import {  Attachment,  Avatar,  messageHasReactions,  MessageOptions,  MessageRepliesCountButton,  MessageStatus,  MessageText,  MessageTimestamp,  ReactionSelector,  SimpleReactionsList,  useMessageContext,} from 'stream-chat-react';
import './CustomMessage.scss';
export const CustomMessage = () => {  const {    isReactionEnabled,    message,    reactionSelectorRef,    showDetailedReactions,  } = useMessageContext();
  const messageWrapperRef = useRef(null);
  const hasReactions = messageHasReactions(message);
  return (    <div className='message-wrapper'>      <Avatar image={message.user?.image} />      <div className='message-wrapper-content'>        <MessageOptions displayLeft={false} messageWrapperRef={messageWrapperRef} />        <div className='message-header'>          <div className='message-header-name'>{message.user?.name}</div>          <div className='message-header-timestamp'>            <MessageTimestamp />          </div>        </div>        {showDetailedReactions && isReactionEnabled && (          <ReactionSelector ref={reactionSelectorRef} />        )}        <MessageText />        <MessageStatus />        {message.attachments && <Attachment attachments={message.attachments} />}        {hasReactions && !showDetailedReactions && isReactionEnabled && <SimpleReactionsList />}        <MessageRepliesCountButton reply_count={message.reply_count} />      </div>    </div>  );};

The Result:#

Custom Message UI Component for Chat

Did you find this page helpful?