The Open Standard for Generative UI
---
title: "React Native"
---
<Steps>
<Step title="Describe the components on the backend" source="https://github.com/thesysdev/openui/blob/main/examples/app-frameworks/react-native/backend/src/library.ts">
Define the component vocabulary (Card, Text, and chart components) with defineComponent and createLibrary in a Node-compatible file, with renderers set to null. The chat route turns it into the system prompt with generateSystemPrompt from @openuidev/lang-core.
```ts title="backend/src/library.ts"
import { createLibrary, defineComponent } from "@openuidev/lang-core";
import { z } from "zod";
// Same names and schemas as the app's library, with null renderers so it runs in Node.
const TextComponent = defineComponent({
name: "Text",
description: "Renders a line of text. Use variant to control emphasis.",
props: z.object({
content: z.string().describe("The text to display"),
variant: z.enum(["body", "heading", "caption"]).optional(),
}),
component: () => null,
});
// BarChart, LineChart and PieChart follow the same pattern and go in Card's children.
const CardComponent = defineComponent({
name: "Card",
description: "A container card that holds a list of child components.",
props: z.object({
children: z.array(TextComponent.ref).optional(),
}),
component: () => null,
});
export const library = createLibrary({
components: [TextComponent, CardComponent],
root: "Card",
});
```
</Step>
<Step title="Stream plain text" source="https://github.com/thesysdev/openui/blob/main/examples/app-frameworks/react-native/backend/src/app/api/chat/route.ts">
Return raw text/plain chunks from the chat route instead of SSE, so the mobile app can read the stream without a browser EventSource.
```ts title="backend/src/app/api/chat/route.ts"
import { generateSystemPrompt } from "@openuidev/lang-core";
import OpenAI from "openai";
import { library } from "@/library"; // The Node-compatible library from step 1.
// cloud: true matches the OpenUI Cloud client below; omit it for other providers.
const SYSTEM_PROMPT = generateSystemPrompt({ cloud: true, library: library.toSpec() });
const openai = new OpenAI({
apiKey: process.env.THESYS_API_KEY,
baseURL: "https://api.thesys.dev/v1/embed",
});
export async function POST(req: Request) {
const { messages } = await req.json();
const completion = await openai.chat.completions.create({
model: "google/gemini-3.6-flash-free",
stream: true,
messages: [{ role: "system", content: SYSTEM_PROMPT }, ...messages],
});
// Write each text delta as a raw chunk, not an SSE event.
const stream = new ReadableStream({
async start(controller) {
for await (const chunk of completion) {
const text = chunk.choices[0]?.delta?.content ?? "";
if (text) controller.enqueue(new TextEncoder().encode(text));
}
controller.close();
},
});
return new Response(stream, {
headers: { "Content-Type": "text/plain; charset=utf-8" },
});
}
```
</Step>
<Step title="Render native components" source="https://github.com/thesysdev/openui/blob/main/examples/app-frameworks/react-native/chat-app/screens/ChatScreen.tsx">
Use Renderer from @openuidev/react-lang in the Expo app with a library that maps the same component names to native views and charts.
```tsx title="chat-app/screens/ChatScreen.tsx"
import { Renderer } from "@openuidev/react-lang";
import React, { useState } from "react";
// Step 1's components with React Native renderers. See library.tsx in the example.
import { library } from "../library";
// Streams the reply with XMLHttpRequest (React Native's fetch has no readable body).
// Copy hooks/useStreamingChat.ts from the example.
import { useStreamingChat } from "../hooks/useStreamingChat";
// Text box and send button. Copy components/ChatInput.tsx from the example.
import { ChatInput } from "../components/ChatInput";
export default function ChatScreen({ backendUrl }: { backendUrl: string }) {
const [response, setResponse] = useState("");
const [isStreaming, setIsStreaming] = useState(false);
const { sendMessage } = useStreamingChat(backendUrl);
async function handleSend(text: string) {
setIsStreaming(true);
// The example also keeps the conversation history for follow-up turns.
await sendMessage([{ role: "user", content: text }], (accumulated, isDone) => {
// `accumulated` is the full reply so far, which is what Renderer expects.
setResponse(accumulated);
setIsStreaming(!isDone);
});
}
return (
<>
<Renderer response={response} library={library} isStreaming={isStreaming} />
<ChatInput onSend={handleSend} disabled={isStreaming} />
</>
);
}
```
</Step>
</Steps>