Open History to see the last commit
1---
2title: A chat box with no tools
3id: ui-recipe-basic-chat
4order: 1
5description: "The smallest working React chat UI. A layout, a message, and a text part."
6keywords:
7 - tanstack ai
8 - createChatHook
9 - chat ui
10 - example
11---
12
13Start here. This chat has no tools, no interrupts, and no styling.
14
15For a Start app with BYOK, open [Basic Chat](../../tutorials/basic-chat).
16
17You need three components: a `layout`, a `message`, and a `text` part. Leave the rest out.
18
19## Server
20
21```tsx
22import { chat, toServerSentEventsResponse } from '@tanstack/ai'
23import { openaiText } from '@tanstack/ai-openai'
24
25export async function POST(request: Request) {
26 const json: unknown = await request.json()
27 if (typeof json !== 'object' || json === null || !('messages' in json)) {
28 return new Response('Invalid body', { status: 400 })
29 }
30 const messages = json.messages
31 const stream = chat({
32 adapter: openaiText('gpt-5.6'),
33 messages: Array.isArray(messages) ? messages : [],
34 })
35 return toServerSentEventsResponse(stream)
36}
37```
38
39## Client
40
41```tsx
42import { fetchServerSentEvents } from '@tanstack/ai-react'
43import { createChatHook } from '@tanstack/ai-react/ui'
44
45const chatOptions = {
46 connection: fetchServerSentEvents('/api/chat'),
47}
48
49const { useAppChat, useChatContext } = createChatHook({
50 options: chatOptions,
51 components: {
52 input: () => {
53 const chat = useChatContext()
54 return (
55 <form
56 onSubmit={(event) => {
57 event.preventDefault()
58 const field = event.currentTarget.elements.namedItem('message')
59 if (!(field instanceof HTMLInputElement)) return
60 const text = field.value.trim()
61 if (!text) return
62 field.value = ''
63 void chat.sendMessage(text)
64 }}
65 >
66 <input name="message" />
67 <button type="submit">Send</button>
68 </form>
69 )
70 },
71 layout: ({ Messages, Input }) => (
72 <main>
73 <Messages />
74 <Input />
75 </main>
76 ),
77 message: ({ message, Parts }) => (
78 <article data-role={message.role}>
79 <Parts />
80 </article>
81 ),
82 },
83 partsComponents: {
84 text: ({ part }) => <p>{part.content}</p>,
85 fallback: () => null,
86 },
87})
88
89export function ChatScreen() {
90 const chat = useAppChat()
91 return <chat.AppChat />
92}
93```
94
95Render `<ChatScreen />`. Type a message. The reply streams into the page.
96
97## What each piece does
98
99`layout` gets `Messages`, `Interrupts`, and `Input` as components. Render them where you want them. `Interrupts` is unused here, so leave it out.
100
101`message` gets one message plus a `Parts` component. `Parts` walks that message and picks a component per part.
102
103`partsComponents.text` renders text parts. `fallback` catches every part type you did not register. It returns `null` here, so anything else is silent.
104
105## Next
106
107- One tool that renders as raw JSON? See [format a tool](./format-a-tool).
108- Need the user to approve something? See [tool approval](./tool-approval).
109- Want markdown with colored fences? See [Highlight markdown code](../markdown).
110- Using Solid, Vue, or Svelte? The same shape applies. See [Solid](../solid), [Vue](../vue), or [Svelte](../svelte).