@chainlit/react-client

中文文档
0.5.0发布于 2026/08/26Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 4664

Websocket client to connect to your chainlit app.

原始使用文档
文档目录7 节
  1. Overview
  2. Installation
  3. Usage
  4. useChatSession
  5. useChatMessages
  6. useChatData
  7. useChatInteract

Overview

The @chainlit/react-client package provides a set of React hooks as well as an API client to connect to your Chainlit application from any React application. The package includes hooks for managing chat sessions, messages, data, and interactions.

Installation

To install the package, run the following command in your project directory:

npm install @chainlit/react-client

This package use Recoil to manage its state. This means you will have to wrap your application in a recoil provider:

import React from 'react';
import ReactDOM from 'react-dom/client';
import { RecoilRoot } from 'recoil';

import { ChainlitAPI, ChainlitContext } from '@chainlit/react-client';

const CHAINLIT_SERVER_URL = 'http://localhost:8000';

const apiClient = new ChainlitAPI(CHAINLIT_SERVER_URL, 'webapp');

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
  <React.StrictMode>
    <ChainlitContext.Provider value={apiClient}>
      <RecoilRoot>
        <MyApp />
      </RecoilRoot>
    </ChainlitContext.Provider>
  </React.StrictMode>
);

Usage

useChatSession

This hook is responsible for managing the chat session's connection to the WebSocket server.

Methods

  • connect: Establishes a connection to the WebSocket server.
  • disconnect: Disconnects from the WebSocket server.
  • setChatProfile: Sets the chat profile state.

Example

import { useChatSession } from '@chainlit/react-client';

const ChatComponent = () => {
  const { connect, disconnect, chatProfile, setChatProfile } = useChatSession();

  // Connect to the WebSocket server
  useEffect(() => {
    connect({
      userEnv: {
        /* user environment variables */
      }
    });

    return () => {
      disconnect();
    };
  }, []);

  // Rest of your component logic
};

useChatMessages

This hook provides access to the chat messages and the first user message.

Properties

  • messages: An array of chat messages.
  • firstUserMessage: The first message from the user.

Example

import { useChatMessages } from '@chainlit/react-client';

const MessagesComponent = () => {
  const { messages, firstUserMessage } = useChatMessages();

  // Render your messages
  return (
    <div>
      {messages.map((message) => (
        <p key={message.id}>{message.output}</p>
      ))}
    </div>
  );
};

useChatData

This hook provides access to various chat-related data and states.

Properties

  • actions: An array of actions.
  • askUser: The current ask user state.
  • avatars: An array of avatar elements.
  • chatSettingsDefaultValue: The default value for chat settings.
  • chatSettingsInputs: The current chat settings inputs.
  • chatSettingsValue: The current value of chat settings.
  • connected: A boolean indicating if the WebSocket connection is established.
  • disabled: A boolean indicating if the chat is disabled.
  • elements: An array of chat elements.
  • error: A boolean indicating if there is an error in the session.
  • loading: A boolean indicating if the chat is in a loading state.
  • tasklists: An array of tasklist elements.

Example

import { useChatData } from '@chainlit/react-client';

const ChatDataComponent = () => {
  const { loading, connected, error } = useChatData();

  // Use the data to render your component
  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error connecting to chat...</p>;
  if (!connected) return <p>Disconnected...</p>;

  // Rest of your component logic
};

useChatInteract

This hook provides methods to interact with the chat, such as sending messages, replying, and updating settings.

Methods

  • callAction: Calls an action.
  • clear: Clears the chat session.
  • replyMessage: Replies to a message.
  • sendMessage: Sends a message.
  • stopTask: Stops the current task.
  • setIdToResume: Sets the ID to resume a thread.
  • updateChatSettings: Updates the chat settings.

Example

import { useChatInteract } from '@chainlit/react-client';

const InteractionComponent = () => {
  const { sendMessage, replyMessage } = useChatInteract();

  const handleSendMessage = () => {
    const message = { output: 'Hello, World!', id: 'message-id' };
    sendMessage(message);
  };

  const handleReplyMessage = () => {
    const message = { output: 'Replying to your message', id: 'reply-id' };
    replyMessage(message);
  };

  // Render your interaction component
  return (
    <div>
      <button onClick={handleSendMessage}>Send Message</button>
      <button onClick={handleReplyMessage}>Reply to Message</button>
    </div>
  );
};

版本与发布记录

@chainlit/react-client 共发布 20 个版本,首次发布于 2023/11/16;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.5.0latest2026/08/262 个月前
0.4.22026/04/226 个月前
0.4.12026/04/087 个月前
0.4.02026/03/247 个月前
0.3.12025/12/2510 个月前
0.3.02025/10/061.1 年前

关于 @chainlit/react-client 的常见问题

@chainlit/react-client 是什么?

Websocket client to connect to your chainlit app.

如何安装 @chainlit/react-client?

在项目目录运行 npm install @chainlit/react-client,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@types/react(^18.3.1)、react(^18.3.1)、react-dom(^18.3.1)、recoil(^0.7.7)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@chainlit/react-client 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@chainlit/react-client 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@chainlit/react-client 可以商用吗?

@chainlit/react-client 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@chainlit/react-client 的下载量有多少?

根据 npm 官方下载统计,@chainlit/react-client 在 2026/10/10 – 2026/11/08 这 30 天内被下载 4,664 次,日均约 155 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@chainlit/react-client 还在维护吗?

最新版本 0.5.0 发布于 2026/08/26(2 个月前);至今共发布 20 个版本,其中过去 12 个月发布了 5 个;首次发布于 2023/11/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。