@basis-theory/basis-theory-react

中文文档
1.32.5发布于 2025/04/22Apache-2.0TypeScriptESM + CJS近 30 天下载 4.8万

Thin React wrapper for BasisTheory JS SDK

原始使用文档
文档目录9 节
  1. Installation
  2. Documentation
  3. Usage
  4. Initialization
  5. Context Provider
  6. Elements
  7. Development
  8. Dependencies
  9. Build the SDK and run Tests

Basis Theory React

Version Downloads Verify

A thin React wrapper for Basis Theory JS SDK.

Installation

Using Node Package Manager

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

Using Yarn

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

Documentation

For a complete list of endpoints and examples, please refer to our React docs

Usage

Initialization

Initializing the SDK is done via calling the useBasisTheory hook with parameters:

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

Context Provider

You can pass the BasisTheoryReact instance down to your component tree using BasisTheoryProvider, and access it later calling the useBasisTheory hook without any parameters:

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

Elements

Elements capabilities are available when passing elements: true in initialization options.

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" />;
};

Development

The provided scripts with the SDK will check for all dependencies, build the solution and run all tests.

Dependencies

Build the SDK and run Tests

Run the following command from the root of the project:

make verify

版本与发布记录

@basis-theory/basis-theory-react 共发布 69 个版本,首次发布于 2021/08/14;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.32.5latest2025/04/221.5 年前
1.32.42025/03/261.6 年前
1.32.32025/03/071.7 年前
1.32.22025/02/221.7 年前
1.32.12025/02/211.7 年前
1.32.02025/02/211.7 年前

其他发布通道:main → 1.0.0-alpha.5,master → 1.0.0-alpha.10。

关于 @basis-theory/basis-theory-react 的常见问题

@basis-theory/basis-theory-react 是什么?

Thin React wrapper for BasisTheory 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 和提交记录判断。