Skip to content
Talk to an engineerRequest access

Render in React

Draw the answer as it streams — prose a word at a time, each component in the place the model put it.

  • ⏱ 10 minutes
  • React 18+
  • Client-side
Terminal window
npm install ./vendor/val-genui/val-genui-protocol-2.0.0.tgz ./vendor/val-genui/val-genui-react-1.0.0.tgz
components/Answer.tsx
import { GenUiReplyView, genUiHeaders } from '@val-genui/react'
import '@val-genui/react/styles.css'
export function Answer({ question }: { question: string }) {
// Once per question — in a handler or a memo, not on every render.
const source = useMemo(
() => fetch('/api/chat', { method: 'POST', headers: genUiHeaders(), body: question }),
[question],
)
return <GenUiReplyView source={source} onAction={(action) => send(action)} />
}

source takes a fetch(…) or its Response, a ReadableStream, or an async iterable (a WebSocket, a framework’s data stream). Or pass reply for a stored answer.

Live — press a button to see the action your app receives

onAction receives { partId, component: { slug, version }, action, payload }. Send it to your backend, where genui.actionMessage(action) turns it into a message for your agent.

const { reply, status, error } = useGenUiReply(source)
reply.parts.map((part) => <GenUiPart key={part.id} part={part} onAction={send} />)
import { RenderWidget } from '@val-genui/react'
<RenderWidget xml={component.xml} data={component.data} theme={component.theme} brightness="dark" />

Markup, data and theme in; elements out. It fetches nothing.

Theme and dark mode: see Theming.