@chartapi/react-connect

中文文档
4.10.0发布于 2026/01/14MITTypeScriptESM + CJS零依赖近 30 天下载 9577

Chart SDK for embedding Chart Connect in React Single Page Applications (SPA)

原始使用文档
文档目录4 节
  1. Install
  2. Usage
  3. Advanced Usage
  4. Passing Metadata

@chartapi/react-connect

NPM

Install

npm install --save @chartapi/react-connect

Usage

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>
  );
};

Advanced Usage

Passing Metadata

You can pass custom metadata as a JSON object that will be included as a stringified query parameter. This is useful for tracking additional context about the connection session:

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

Note: The metadata object is JSON-stringified and passed as a URL search parameter. Avoid passing large objects or non-serializable values (e.g., functions, circular references).

版本与发布记录

@chartapi/react-connect 共发布 21 个版本,首次发布于 2024/03/05;过去 12 个月发布了 4 个版本。

版本发布日期距今
4.10.0latest2026/01/149 个月前
4.9.02026/01/109 个月前
4.8.02026/01/109 个月前
4.7.02025/11/2611 个月前
4.5.02025/08/191.2 年前
4.4.02025/08/191.2 年前

关于 @chartapi/react-connect 的常见问题

@chartapi/react-connect 是什么?

Chart SDK for embedding Chart Connect in React Single Page Applications (SPA)

如何安装 @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 和提交记录判断。