when UI libs meet xsai
npm install @xsai-use/reactReact bindings for xsAI.
This package provides a collection of React hooks for building interactive web applications with powerful features and minimal boilerplate.
``bash`
npm install @xsai-use/reactor
yarn add @xsai-use/reactor
pnpm add @xsai-use/react
- useChat: hook for building chat interfaces
__parameter__
| Parameter | Type | Description |
|-----------|------|-------------|
| id | string | Unique identifier for the chat |
| generateID? | () => string | Function to generate unique IDs for messages |
| initialMessages? | Message[] | Initial messages to populate the chat |
| onFinish? | () => void | Callback when the chat response completes |
| preventDefault? | boolean | Whether to prevent default form submission |
__return__
| Property | Type | Description |
|----------|------|-------------|
| error | Error \| null | Error object if an error occurred |
| handleInputChange | (e: React.ChangeEvent
| handleSubmit | (e?: React.FormEvent
| input | string | Current input value |
| messages | UIMessage[] | Array of messages in the conversation |
| setMessages | (messages: UIMessage[]) => void | Function to set messages manually |
| reload | (id?: string) => Promise
| reset | () => void | Resets the chat to initial state |
| setInput | (input: string) => void | Function to set input value |
| status | 'idle' \| 'loading' \| 'error' | Current status of the chat |
| stop | () => void | Stops the current response generation |
| submitMessage | (message: InputMessage) => Promise
More examples in examples
`jsx
import { useChat } from '@xsai-use/react'
export function ChatComponent() {
const {
handleSubmit,
handleInputChange,
input,
messages,
status,
error,
reset,
stop,
reload,
} = useChat({
id: 'simple-chat',
preventDefault: true,
initialMessages: [
{
role: 'system',
content: 'you are a helpful assistant.',
},
],
baseURL: 'https://api.openai.com/v1/',
model: 'gpt-4.1',
maxSteps: 3,
})
return (
MIT