Skip to content

在 React 中使用完整 UI SDK ​

适用于 HENGSHI SENSE 7.0。使用部署提供的 hengshi-copilot@VERSION.js(版本获取方式见获取 SDK 文件与版本),在 React 容器挂载后创建实例,卸载时销毁。SDK 自带对话 UI,不需要把内部包作为 npm 依赖引入。

先加载 script,并完成 认证、CORS 和 完整 UI SDK 配置。

jsx
import { useEffect, useRef } from 'react';

export function DataAgent() {
  const container = useRef(null);
  useEffect(() => {
    const copilot = new window.Copilot(container.current, {
      baseUrl: 'https://analytics.example.com',
      locale: 'zh-CN',
      userConfig: { dataSources: [{ dataAppId: 251, datasetId: 1 }] },
      style: { right: 20, bottom: 20, width: 440, height: 640 },
    });
    return () => copilot.destroy();
  }, []);
  return <div ref={container} />;
}

SPA 配置更新使用实例的 update(),不要用配置对象身份变化反复创建实例。已有对话的范围不会因此改变,需要新建对话。完全自定义 React 界面时使用 数据客户端 SDK。

衡石分析平台使用手册