Skip to main content
Saved

Pattern Components Intermediate

Message Thread

You can store a conversation as an ordered array of messages, each with a stable id, a role, its content, and its own status. That way, streaming and errors affect a single turn instead of the whole screen.

Examples
React, Vue, Svelte and native web
Read
4 min

The problem

Your first version of a chat UI probably stores the conversation as two strings, one for the user’s last prompt and one for the assistant’s last reply. That works for the first exchange, but as soon as the user sends a second message you need the whole history, and two strings can’t store it.

The pattern

You can store the conversation as an ordered array of message objects. Each message has a stable id, a role of user, assistant, or system, its content, and a status such as streaming, complete, stopped, or error. The id is generated when the message is created, instead of being taken from the array index. Each time the user sends a prompt, the app adds a user message and an empty assistant message with a streaming status. The Streaming Response loop finds that assistant message by its id and appends tokens to it, and when the response finishes, the loop updates the message’s status.

Problem

It gets worse if you put isStreaming or error in one place at the top of the component. Regenerating the third message might then flip a flag that the fifth message also reads, and suddenly two bubbles show a spinner, or a failure on one turn might show an error across the whole transcript. A chat UI has states like thinking, streaming, stopped, and failed, and if you don’t store them on each message, a change to one message’s state shows up on other messages too.

A conversation is really a list of turns, and each turn has an author and its own state, such as still streaming, finished, or failed. If you store it as anything simpler, each feature you add later is harder to build.

Solution

Because each message stores its own state, other features can work on just the message they’re meant for. A Thinking Indicator renders for the one message that’s streaming with empty content, and Response Regeneration replaces a single message without touching the messages around it.

The list is keyed by message id, so the framework can reuse the same DOM nodes as a message’s content grows. The container has the ARIA log role, so assistive technology treats it as a transcript, and a separate live region announces each finished reply once. Very long conversations can use Virtual Scrolling, but it’s only worth it once you have hundreds of messages, because virtualization makes the auto-scrolling that streaming needs more complicated.

Example

The examples below cover the message model and reducer, rendering the transcript in each framework, styling each message by role and status, and announcing new turns to screen readers.

The Conversation Model

A small reducer keeps all the changes to the conversation in one place, including adding a turn, streaming tokens into it, and marking it as finished.

function conversationReducer(messages, action) {
  switch (action.type) {
    case 'send':
      return [
        ...messages,
        { id: crypto.randomUUID(), role: 'user', content: action.text, status: 'complete' },
        { id: action.replyId, role: 'assistant', content: '', status: 'streaming' },
      ];

    case 'token': // append a streamed chunk to one assistant message
      return messages.map(m =>
        m.id === action.id ? { ...m, content: m.content + action.chunk } : m
      );

    case 'finish': // 'complete' | 'stopped' | 'error'
      return messages.map(m =>
        m.id === action.id ? { ...m, status: action.status } : m
      );

    default:
      return messages;
  }
}

Rendering the Transcript

function MessageThread({ messages }) {
  return (
    <div className="thread" role="log" aria-live="off" aria-label="Conversation">
      {messages.map(m => (
        <article key={m.id} className={`message message--${m.role}`} data-status={m.status}>
          <span className="role">{m.role === 'user' ? 'You' : 'Assistant'}</span>
          <div className="content">
            {m.content || (m.status === 'streaming' && <ThinkingIndicator />)}
          </div>
          {m.status === 'error' && <RetryButton id={m.id} />}
        </article>
      ))}
    </div>
  );
}

Distinguishing Turns Visually and Semantically

Each message is an <article> with a visible role label, so a screen reader user can move from turn to turn and hear who wrote each one. The CSS sets each message’s alignment and bubble corners from its role, and adds an outline or a suffix to show its status.

.message { display: grid; gap: 0.25rem; padding: 0.75rem 1rem; }
.message--user { justify-items: end; }
.message--user .content { background: var(--accent); color: #fff; border-radius: 1rem 1rem 0 1rem; }
.message--assistant .content { background: var(--surface); border-radius: 1rem 1rem 1rem 0; }
.message[data-status="error"] { outline: 1px solid var(--danger); }
.message[data-status="stopped"] .content::after { content: " (stopped)"; opacity: 0.6; }

Announcing New Turns Once

A log is a live region by default, with aria-live="polite", so every streamed token counts as a change, and a screen reader can end up reading the reply out in fragments or falling behind. Nesting another live region inside the log, or switching a message’s aria-live from off to polite when it finishes, doesn’t fix this. Screen readers handle nested live regions inconsistently, and changing aria-live doesn’t announce text that’s already in the element. That’s why the transcript above sets aria-live="off" on the log, and a separate, visually hidden status region announces each reply once, when the finish action marks it complete.

function ThreadAnnouncer({ messages }) {
  const [announcement, setAnnouncement] = useState('');
  const last = messages[messages.length - 1];

  useEffect(() => {
    // Announce the reply once it's complete, not on every token
    if (last?.role === 'assistant' && last.status === 'complete') {
      setAnnouncement(last.content);
    }
  }, [last?.id, last?.status]);

  return (
    <div className="sr-only" role="status" aria-live="polite">
      {announcement}
    </div>
  );
}

Benefits

  • A streaming reply or an error only changes its own message, so other bubbles don’t show a spinner or an error by mistake.
  • With stable ids, the framework can reuse the same DOM nodes as a message grows, which keeps streaming smooth and keeps the user’s focus and text selection in place.
  • History, editing, branching, and regeneration all become simple changes to a list, instead of special cases.
  • The log role lets assistive technology users move through the transcript, and a single status region announces each finished reply.
  • A plain array of messages is easy to save and reload, and tests can replay it from a fixture.

Tradeoffs

  • It’s tempting to add a top-level isLoading flag next to the array, and doing that brings back the problem of one message’s state affecting the others.
  • You have to set up announcements carefully, or a screen reader might miss new turns or get flooded with fragments of tokens.
  • Very long threads might eventually need virtualization, which makes it harder to keep the view scrolled to the bottom while a reply streams in.
  • If messages contain rich content, such as markdown, code, tool calls, and images, each bubble needs its own rendering logic instead of just showing a plain string.
  • Generating stable ids and passing them through to the streaming code takes a little more code than just joining strings together.

Summary

With this pattern, you store a conversation as an ordered list of messages, and each one has a role, a stable id, and its own status. Streaming or regenerating a reply then only changes one turn, and the log role keeps the whole transcript accessible. It takes more work up front than two strings, but most of the other features in a chat UI are built on top of it.

See also

Next in Components Presentational vs. Container Presentational components render the UI, and container components manage the data and business logic behind it.

A monthly email
from Den Odell.

What I'm thinking about frontend patterns, plus news when the site changes.

No spam. Unsubscribe anytime.