# @chartapi/react-connect 中文文档

规范地址：https://npmcn.com/package/%40chartapi/react-connect/

版本：4\.10\.0
npm 元数据更新时间：2026-01-13T19:27:08.851Z
中文译文更新时间：2026-11-09T11:01:56.164Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40chartapi/react-connect
原文对照：https://npmcn.com/package/%40chartapi/react-connect/?lang=original
项目主页：https://trychart.com/
源码仓库：https://github.com/getcharteditor/react-connect

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：679 kB，28 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=8
- 运行依赖：0 个
- peer 依赖：react \>=16\.9\.0
- 首次发布：2024-03-04T19:37:35.519Z
- 最新版本发布：2026-01-13T19:27:08.626Z
- 近 30 天下载量：9577（2026-10-10 至 2026-11-08，npm 官方下载统计）
- 已发布版本数：21（近 12 个月 4 个）

## 中文简介

在 React 单页应用程序中嵌入图连接的图 SDK

## 安装

```sh
npm install @chartapi/react-connect
```

也可以使用：`pnpm add @chartapi/react-connect`、`yarn add @chartapi/react-connect`、`bun add @chartapi/react-connect`

## README

# @ chartapi/ 反应连接

[![NPM](https://img.shields.io/npm/v/@chartapi/react-connect)](https://www.npmjs.com/package/@chartapi/react-connect)

## 安装

```bash
npm install --save @chartapi/react-connect
```

## 使用量

```jsx
import React, { useState } from 'react';
import { useChartConnect } from '@chartapi/react-connect';

const App = () => {
  const [code, setCode] = useState(null);

  const onSuccess = ({ code }) => setCode(code);
  const onError = ({ errorMessage }) => console.error(errorMessage);
  const onClose = () => console.log('User exited Chart Connect');

  const { open } = useChartConnect({
    clientId: '<your-client-id>',
    // zIndex: 999,
    // sessionSettingsId: '<your-session-settings-id>', // optional; if provided, will prefill information in the form
    // flow: 'irs-8821', // optional; if provided, will set the flow to the specified flow override the master settings
    // providers: ['irs', 'ssa'], // optional; if provided, will set the providers to the specified providers override the master 
    // overlay: `rgba(199,201,199,0.5)` // optional; if provided, will set the overlay to the specified overlay CSS attribute
    // metadata: { userId: '123', source: 'web' }, // optional; any JSON object that will be passed as a stringified query parameter

    onSuccess,
    onError,
    onClose,
  });

  return (
    <div>
      <header>
        <p>Code: {code}</p>
        <button type="button" onClick={() => open()}>
          Open Chart Connect
        </button>
      </header>
    </div>
  );
};
```

## 高级使用

### 传递元数据

您可以通过自定义元数据作为JSON对象,该对象将作为字符串化的查询参数包含. 用于跟踪连接会话的其他上下文:

```jsx
const { open } = useChartConnect({
  clientId: '<your-client-id>',
  metadata: {
    userId: '12345',
    source: 'dashboard',
    campaign: 'spring-2024',
  },
  onSuccess,
  onError,
  onClose,
});
```

**说明:** 元数据对象为JSON字符串并被作为URL搜索参数通过. 避免传递大对象或非序列值(如函数,循环引用).

## 常见问题

### @chartapi/react-connect 是什么？

在 React 单页应用程序中嵌入图连接的图 SDK

### 如何安装 @chartapi/react-connect？

在项目目录运行 npm install @chartapi/react-connect，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：react（\>=16\.9\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @chartapi/react-connect 支持 TypeScript 吗？

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

### @chartapi/react-connect 支持 ESM 还是 CommonJS？

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

### @chartapi/react-connect 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=8。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @chartapi/react-connect 可以商用吗？

@chartapi/react-connect 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @chartapi/react-connect 的下载量有多少？

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

### @chartapi/react-connect 还在维护吗？

最新版本 4\.10\.0 发布于 2026/01/14（9 个月前）；至今共发布 21 个版本，其中过去 12 个月发布了 4 个；首次发布于 2024/03/05。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

