The Vercel AI SDK · Chapter 8 of 21
useChat: a streaming chat in React
By the end of this chapter you can build a chat page in React with the AI SDK's useChat hook: messages that stream in word by word, a message box that knows when Mali is busy, and a Stop button. You will write a component for one message, a component for the message box, and the chat that joins them to the route from chapter 7. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The chat UI
- 3 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of The Vercel AI SDK are free. This one comes with the all labs plan.
Why you need this
The route works: lab stream shows Mali's answer arriving as events, a token every 15 ms. But a customer will never run lab stream. Ananya wants a chat box on HariBhari's website that shows the answer the way the events arrive: the first words at once, the rest as they are written, and a way to stop a long answer the customer does not need.
You could write that page by hand with chapter 7's stream reader: post the chat, read events, find the right message, add each delta to the right part, keep the list of messages, know when a reply is still coming, abort the request on Stop. Farhan has seen that code grow to a few hundred lines in every team that wrote it. The AI SDK has it in one hook.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Message.tsx: one message on the page
Export default function Message({ message }: { message: UIMessage }) from Message.tsx. Show a label, You for a user message and Mali for an assistant message, then the text of every part of message.parts whose type is 'text', in order, each with a key. Show nothing for other parts.
The Check shows a user message, and an assistant message with a step-start part, two text parts and a data part.
- Task 2
Composer.tsx: the message box
Export default function Composer({ status, onSend, onStop }) from Composer.tsx (status: ChatStatus from 'ai', onSend: (text: string) => void, onStop: () => void). A <form> with an <input aria-label="Message"> and a submit button Send. On submit, if the text is not empty after trim and status is 'ready', call onSend(text) once and clear the box.
Send is disabled unless status is 'ready'. While status is 'submitted' or 'streaming', also show a button Stop that calls onStop; no Stop for 'ready' or 'error'.
- Task 3
Chat.tsx: the chat
Export default function Chat() from Chat.tsx. Call useChat (from '@ai-sdk/react') with transport: new DefaultChatTransport({ api: '/api/chat' }). Show every message with your Message (key m.id), then your Composer with status, onSend calling sendMessage({ text }) and onStop calling stop.
The Check shows your chat in a page at the lab model's real pace, through the given route: the customer's message appears at once, Mali's long answer grows over more than 10 changes, Send is disabled and Stop shown while it streams, a second message posts all 3 messages, and Stop ends an answer early and makes Send usable again.
What you will learn
- useChat: what it keeps (messages, status) and what it does (posts, reads the stream, builds parts).
- DefaultChatTransport: how useChat talks to /api/chat.
- Rendering message.parts, and why there is no message.content.
- status: submitted, streaming, ready, error, and what the page does in each.
- sendMessage and stop, and what they send to the route.
- lab ui: watching your component in a page from the terminal.
More in The chat UI
Build "useChat: a streaming chat in React" yourself
HariBhari, a Bengaluru plant shop, wants Mali, a plant helper on its website. You build it end to end, from the first call to a chat that streams cards, asks before it adds to the cart and stays within budget. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of The Vercel AI SDK are free. This one comes with the all labs plan.