Skip to content

useChat

useChat(options): UseChatReturn

Defined in: use-chat.ts:94

useChat - React hook for chat interfaces.

Provides state management, streaming, and utilities for building chat applications with AI backends.

Supports two modes:

  1. HTTP Mode (default): Uses api endpoint with fetch
  2. Direct Mode: Uses direct.backend for direct backend access

UseChatOptions = {}

UseChatReturn

import { useChat } from 'ai.matey.react.core';
function ChatComponent() {
const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat({
api: '/api/chat',
});
return (
<div>
{messages.map((m) => (
<div key={m.id}>{m.role}: {m.content}</div>
))}
<form onSubmit={handleSubmit}>
<input value={input} onChange={handleInputChange} />
<button type="submit" disabled={isLoading}>Send</button>
</form>
</div>
);
}
import { useChat } from 'ai.matey.react.core';
import { AnthropicBackend } from 'ai.matey.backend/anthropic';
const backend = new AnthropicBackend({ apiKey: process.env.ANTHROPIC_API_KEY });
function ChatComponent() {
const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat({
direct: {
backend,
systemPrompt: 'You are a helpful assistant.',
},
});
return (
<div>
{messages.map((m) => (
<div key={m.id}>{m.role}: {m.content}</div>
))}
<form onSubmit={handleSubmit}>
<input value={input} onChange={handleInputChange} />
<button type="submit" disabled={isLoading}>Send</button>
</form>
</div>
);
}