# @basis-theory/basis-theory-react 中文文档

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

版本：1\.32\.5
npm 元数据更新时间：2026-11-02T18:33:31.127Z
中文译文更新时间：2026-11-07T18:02:34.230Z
软件包许可：Apache-2\.0

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

原始 npm 页面：https://www.npmjs.com/package/%40basis-theory/basis-theory-react
原文对照：https://npmcn.com/package/%40basis-theory/basis-theory-react/?lang=original
项目主页：https://github.com/Basis-Theory/basis-theory-react#readme
源码仓库：https://github.com/Basis-Theory/basis-theory-react

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

- 包体积（解压后）：58.5 kB，33 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=10\.12\.0
- 运行依赖：1 个
- peer 依赖：react ^16\.8\.0 \|\| ^17\.0\.0 \|\| ^18\.0\.0 \|\| ^19\.0\.0，react-dom ^16\.8\.0 \|\| ^17\.0\.0 \|\| ^18\.0\.0 \|\| ^19\.0\.0
- 首次发布：2021-08-13T18:21:49.221Z
- 最新版本发布：2025-04-21T18:22:51.526Z
- 近 30 天下载量：47911（2026-10-08 至 2026-11-06，npm 官方下载统计）
- 已发布版本数：69（近 12 个月 0 个）

## 中文简介

Basic Theory JS SDK的瘦回放包装

## 安装

```sh
npm install @basis-theory/basis-theory-react
```

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

## README

# 基础理论反应

[![Version](https://img.shields.io/npm/v/@basis-theory/basis-theory-react.svg)](https://www.npmjs.org/package/@basis-theory/basis-theory-react) [![Downloads](https://img.shields.io/npm/dm/@basis-theory/basis-theory-react.svg)](https://www.npmjs.org/package/@basis-theory/basis-theory-react) [![Verify](https://github.com/Basis-Theory/basis-theory-react/actions/workflows/release.yml/badge.svg)](https://github.com/Basis-Theory/basis-theory-react/actions/workflows/release.yml)

薄反射包装 [基础理论](https://basistheory.com/) JS SDK(英语:JS SDK).

## 安装

使用 [节点软件包管理器](https://docs.npmjs.com/)

```sh
npm install --save @basis-theory/basis-theory-react
```

使用 [雅伦](https://classic.yarnpkg.com/en/docs/)

```sh
yarn add @basis-theory/basis-theory-react
```

## 文档

关于终点和例子的完整清单,请参见我们 [反应文件](https://developers.basistheory.com/docs/sdks/web/react/)

## 使用量

### 初始化

SDK的初始化是通过调用 `useBasisTheory` 有参数的钩子 :

```jsx
import { useBasisTheory } from '@basis-theory/basis-theory-react';

export default function MyWrapper() {
  const { bt, error } = useBasisTheory('<Public API Key>'); // replace with your application key

  // instance stays undefined during initialization
  if (bt) {
    // able to call BasisTheory methods
  }

  if (error) {
    // initialization error
  }
}
```

### 上下文提供者

你可以通过 `BasisTheoryReact` 下到组件树 `BasisTheoryProvider`,然后访问它以后叫 `useBasisTheory` 无任何参数的钩子 :

```jsx
import {
  BasisTheoryProvider,
  useBasisTheory,
} from '@basis-theory/basis-theory-react';

const App = () => {
  const { bt } = useBasisTheory('<Public API Key>', {
    elements: true,
  });

  return (
    <BasisTheoryProvider bt={bt}>
      <MyComponent />
    </BasisTheoryProvider>
  );
};

const MyComponent = () => {
  // calling this hook with no attributes grabs the instance from Context
  const { bt } = useBasisTheory();

  return <div>My content</div>;
};
```

### 要件

[要件](https://developers.basistheory.com/docs/sdks/web/react/#initialization) 在通过时具备能力 `elements: true` 在初始化选项中.

```jsx
import {
  BasisTheoryProvider,
  TextElement,
  useBasisTheory,
} from '@basis-theory/basis-theory-react';

const App = () => {
  const { bt } = useBasisTheory('<Public API Key>', {
    elements: true,
  });

  return (
    <BasisTheoryProvider bt={bt}>
      <MyComponent />
    </BasisTheoryProvider>
  );
};

const MyComponent = () => {
  // calling this hook with no attributes grabs the instance from Context
  const { bt } = useBasisTheory();

  return <TextElement id="myInput" />;
};
```

## 发展

与 SDK 提供的脚本将检查所有依赖,构建解决方案并运行所有测试.

### 依赖关系

-   [节点JS](https://nodejs.org/en/) > 10.12.0
-   [雅伦](https://classic.yarnpkg.com/en/docs/)

### 构建 SDK 并运行测试

从工程根部运行以下命令 :

```sh
make verify
```

## 常见问题

### @basis-theory/basis-theory-react 是什么？

Basic Theory JS SDK的瘦回放包装

### 如何安装 @basis-theory/basis-theory-react？

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

### @basis-theory/basis-theory-react 支持 TypeScript 吗？

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

### @basis-theory/basis-theory-react 支持 ESM 还是 CommonJS？

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

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

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

### @basis-theory/basis-theory-react 可以商用吗？

@basis-theory/basis-theory-react 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @basis-theory/basis-theory-react 的下载量有多少？

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

### @basis-theory/basis-theory-react 还在维护吗？

最新版本 1\.32\.5 发布于 2025/04/22（1\.5 年前）；至今共发布 69 个版本，其中过去 12 个月发布了 0 个；首次发布于 2021/08/14。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

