@codatum/embed-react

中文文档
1.0.0发布于 2026/03/03Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 2771

React integration for Codatum signed embedding

原始使用文档
文档目录10 节
  1. Installation
  2. Usage
  3. Calling reload from the parent
  4. Custom loading UI
  5. Changing props at runtime
  6. API
  7. Props
  8. Callbacks
  9. Ref ( EmbedReactRef )
  10. See also

@codatum/embed-react

React integration for Codatum Signed Embed. Provides a single component that wraps the core SDK. All of @codatum/embed is re-exported so you can use this package as a single entry point (one version, no split).

For options, types, events, and programmatic API details, see @codatum/embed.

Installation

pnpm add @codatum/embed-react
# or
npm install @codatum/embed-react

Requires React 18+ (peer dependency).

Usage

import { EmbedReact } from "@codatum/embed-react";

const embedUrl = "https://app.codatum.com/protected/workspace/xxx/notebook/yyy";
const tokenProvider = async () => {
  const res = await fetch("/api/embed-token", { method: "POST" });
  const data = await res.json();
  return { token: data.token };
};

function App() {
  return (
    <EmbedReact
      embedUrl={embedUrl}
      tokenProvider={tokenProvider}
      iframeOptions={{ theme: "LIGHT", locale: "ja" }}
      onStatusChanged={(e) => console.log("Status", e.status)}
      onParamChanged={(e) => console.log("Params", e.params)}
      onExecuteSqlsTriggered={(e) => console.log("Execute", e.params)}
      onError={(e) => console.error(e)}
    />
  );
}

Calling reload from the parent

Use a ref and call the exposed reload() method. It returns true on success and false on failure (errors are reported via onError; it does not throw).

import { useRef } from "react";
import { EmbedReact, type EmbedReactRef } from "@codatum/embed-react";

function App() {
  const embedRef = useRef<EmbedReactRef>(null);

  async function onFilterChange() {
    const ok = await embedRef.current?.reload();
    if (!ok) console.warn("reload failed — see onError");
  }

  return (
    <>
      <button type="button" onClick={onFilterChange}>
        Reload
      </button>
      <EmbedReact
        ref={embedRef}
        embedUrl="..."
        tokenProvider={...}
        onError={handleError}
      />
    </>
  );
}

Custom loading UI

Use renderLoading to show custom UI while loading. showLoadingOn controls which statuses show it (default: INITIALIZING / RELOADING / REFRESHING).

<EmbedReact
  embedUrl={embedUrl}
  tokenProvider={tokenProvider}
  renderLoading={() => <MySpinner />}
/>

Show only on first load:

<EmbedReact
  embedUrl={embedUrl}
  tokenProvider={tokenProvider}
  showLoadingOn={["INITIALIZING"]}
  renderLoading={() => <FullPageLoader />}
/>

Branch by status:

<EmbedReact
  embedUrl={embedUrl}
  tokenProvider={tokenProvider}
  renderLoading={({ status }) =>
    status === "INITIALIZING" ? <FullPageLoader /> : <SubtleBar />
  }
/>

Changing props at runtime

Props are read once at mount. To apply new values (e.g. a different embedUrl or iframeOptions), use key to force a remount:

<EmbedReact key={embedUrl} embedUrl={embedUrl} tokenProvider={tokenProvider} />

API

Option types and behavior (e.g. iframeOptions, tokenOptions, displayOptions, devOptions) are the same as in @codatum/embed. The component uses its root element as the iframe container (no container prop).

Props

PropRequiredType
embedUrlYesstring
tokenProviderYes(context: TokenProviderContext) => Promise<TokenProviderResult>
iframeOptionsNoIframeOptions
tokenOptionsNoTokenOptions
displayOptionsNoDisplayOptions
devOptionsNoDevOptions
showLoadingOnNoEmbedStatus[] — Which statuses show the loading overlay. Default: ['INITIALIZING', 'RELOADING', 'REFRESHING']. Ignored when renderLoading is not set.
renderLoadingNo(props: { status: EmbedStatus }) => ReactNode — Custom UI shown while loading. When not set, the iframe's built-in loading is used.

Callbacks

CallbackPayloadWhen
onStatusChanged(payload: { type: 'STATUS_CHANGED', status: EmbedStatus, previousStatus: EmbedStatus }) => voidSee core SDK
onParamChanged(payload: { type: 'PARAM_CHANGED', params: EncodedParam[] }) => voidSee core SDK
onExecuteSqlsTriggered(payload: { type: 'EXECUTE_SQLS_TRIGGERED', params: EncodedParam[] }) => voidSee core SDK
onExecutionSucceeded(payload: { type: 'EXECUTION_SUCCEEDED' }) => voidSee core SDK
onExecutionFailed(payload: { type: 'EXECUTION_FAILED', errorMessage: string }) => voidSee core SDK
onError(err: EmbedError) => voidInit, reload, or token auto-refresh failed

Ref (EmbedReactRef)

PropertyTypeDescription
reload()() => Promise<boolean>Re-fetches token and params; returns false on failure (error reported via onError)
status'CREATED' | 'INITIALIZING' | 'RELOADING' | 'REFRESHING' | 'READY' | 'DESTROYED'Current embed state

Need a custom container or full control? Use createEmbed() from this package (or @codatum/embed) inside a useEffect and call instance.destroy() in the cleanup.

See also

版本与发布记录

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

版本发布日期距今
1.0.0latest2026/03/038 个月前
0.5.22026/03/038 个月前
0.5.12026/03/038 个月前
0.5.02026/03/038 个月前
0.4.02026/03/028 个月前
0.3.02026/02/248 个月前

关于 @codatum/embed-react 的常见问题

@codatum/embed-react 是什么?

React integration for Codatum signed embedding

如何安装 @codatum/embed-react?

在项目目录运行 npm install @codatum/embed-react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@codatum/embed-react 支持 TypeScript 吗?

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

@codatum/embed-react 支持 ESM 还是 CommonJS?

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

@codatum/embed-react 需要什么版本的 Node.js?

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

@codatum/embed-react 可以商用吗?

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

@codatum/embed-react 的下载量有多少?

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

@codatum/embed-react 还在维护吗?

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