Skip to main content

React Native integration

@distri/react-native renders an agent chat with native components. It shares agent and chat behaviour with @distri/react through @distri/core and @distri/state, and does not import browser UI, DOM APIs, Tailwind or React DOM.

The code below comes from the package README and the react-native-chat sample, an Expo app. For the web version of the same steps, see React integration.

Before you start​

  • An agent in your workspace: Getting Started applies one.
  • A way for the app to get a short-lived user access token from your server. Keep API keys and refresh tokens on a trusted server or in platform secure storage, never in the JavaScript bundle or AsyncStorage. The React guide shows a server-side token exchange.

1. Install the packages​

npm install @distri/core @distri/react-native

The package expects react and react-native as peer dependencies.

2. Provide a streaming fetch​

Agent chat streams its responses, so the client needs a Fetch implementation whose response body supports body.getReader(), plus TextDecoder. In Expo, pass fetch from expo/fetch as fetchImpl. In a bare React Native app, confirm that your networking implementation supports streamed POST responses before enabling chat.

3. Wrap the app in DistriNativeProvider​

From the package README:

import { fetch as expoFetch } from 'expo/fetch';
import { Chat, DistriNativeProvider, useAgent } from '@distri/react-native';

export function App() {
return (
<DistriNativeProvider config={{
baseUrl: 'https://api.distri.dev/v1',
accessToken: 'short-lived-user-token',
fetchImpl: expoFetch,
}}>
<AgentChat />
</DistriNativeProvider>
);
}

The sample builds the config from what the user entered and adds a workspace id (sent as X-Workspace-Id) and a timeout:

const config = useMemo(() => ({
baseUrl: baseUrl.trim().replace(/\/$/, ''),
accessToken,
workspaceId: workspaceId.trim(),
fetchImpl: expoFetch,
timeout: 30000,
}), [accessToken, baseUrl, workspaceId]);

accessToken is held in the DistriClient instance; use onTokenRefresh to coordinate refreshes.

4. Load the agent and render <Chat>​

useAgent takes the agent's name as agentIdOrDef and returns the agent with its loading and error state. From App.tsx in the sample:

function AgentChat({ agentId }: { agentId: string }) {
const { agent, loading, error } = useAgent({ agentIdOrDef: agentId });
if (loading) return <Text style={styles.status}>Loading agent…</Text>;
if (error) return <Text style={styles.error}>{error.message}</Text>;
if (!agent) return <Text style={styles.status}>Agent not found or unavailable.</Text>;
return <Chat agent={agent} threadId={`native-${agentId}`} />;
}

Chat uses a native FlatList, a multiline composer, a stop control and incremental assistant text. Assistant text renders as Markdown.

5. Add client tools, context and custom renderers​

Tools run in the app, and beforeSendMessage attaches app state to each message without showing it as user-authored text. From the package README:

const tools = [{
name: 'update_profile',
type: 'function' as const,
autoExecute: true,
description: 'Update a profile field',
parameters: { type: 'object', properties: { field: { type: 'string' }, value: { type: 'string' } } },
handler: async ({ field, value }: { field: string; value: string }) => {
await saveProfileField(field, value);
return { saved: true };
},
}];

<Chat
agent={agent}
threadId={threadId}
externalTools={tools}
beforeSendMessage={async message => ({
...message,
parts: [...message.parts, {
part_type: 'text',
data: `Current profile: ${JSON.stringify(profile)}`,
}],
metadata: {
...message.metadata,
parts: { ...message.metadata?.parts, [message.parts.length]: { developer: true } },
},
})}
toolRenderers={{
update_profile: ({ toolCall, completeTool }) => (
<ProfileUpdateCard call={toolCall} onComplete={completeTool} />
),
}}
rendering="rich"
/>
  • externalTools: the agent's in-product tools. Tools without autoExecute show native approve and decline actions.
  • toolRenderers: replaces how one tool's call is shown; the tool in externalTools still executes.
  • rendering="rich" adds tool input and result detail and action controls; the default, minimal, keeps tool summaries compact.

6. Let users attach images​

onPickImage on Chat adds an attach button. Return { uri, base64, mimeType, name } from your picker (for example expo-image-picker with base64: true); images are sent as image parts in the same shape @distri/react sends.

7. Theme it​

Every renderer reads tokens from the theme prop of DistriNativeProvider. Pass only what differs; styles overrides individual slots after the tokens are applied:

<DistriNativeProvider
config={config}
theme={{
colors: { primary: '#6a3ad5', userBubble: '#f1ebff', userText: '#39266c', assistantBubble: '#fffdf9' },
fonts: { body: 'Quicksand', heading: 'Outfit' },
radii: { bubble: 14, input: 15 },
styles: { sendButton: { width: 44 } },
}}
>

Slots: list, userBubble, assistantBubble, userText, assistantText, composer, input, sendButton, sendButtonText, toolCard, loadingStrip. useDistriTheme() gives your own components the same tokens.

Run the sample​

From a checkout of distrihub/distrijs:

pnpm install
pnpm --filter @distri/react-native build
pnpm --filter @distri/react-native-chat-sample start

The app asks for the API URL, the agent id, the workspace id and a short-lived access token, and keeps the token only in React state. Do not put API keys, client secrets or access tokens in EXPO_PUBLIC_* variables; EXPO_PUBLIC_DISTRI_BASE_URL, EXPO_PUBLIC_DISTRI_AGENT_ID and EXPO_PUBLIC_DISTRI_WORKSPACE_ID are non-secret defaults for the form.

The sample also has an on-device Storybook that runs against offline fixtures, with no server or token:

pnpm --filter @distri/react-native-chat-sample storybook

What is web-only​

The native package does not include browser-only iframe embedding for live_view, hover interactions, DOM-based form scraping, Tailwind styles, the web replay scrubber, or the web feedback and read-tracking widgets. On native, live views open through Linking, and app context is passed explicitly with beforeSendMessage.

See also​