@gocardless/react-dropin

中文文档
0.3.6发布于 2025/08/26MITTypeScriptESM + CJS近 30 天下载 7.4万

React bindings to the GoCardless Dropin checkout flow

原始使用文档
文档目录4 节
  1. Installation
  2. Examples
  3. Storybook
  4. Publishing

GoCardless React Dropin

npm version

React bindings for the GoCardless Dropin checkout flow.

Installation

With npm:

npm install --save @gocardless/react-dropin

With yarn:

yarn add @gocardless/react-dropin

Examples

This library exports React hook functions that you can use to trigger a GoCardless Dropin instance.

Here is a simple example of an App that wants to create a Billing Request Flow ID via its backend API, then provide a DropinButton that the payer can click to trigger the Dropin.

See this in action at the GoCardlessDropinButton story

import React, { useCallback, useState } from "react";
import {
  useGoCardlessDropin,
  GoCardlessDropinOptions,
  GoCardlessDropinOnSuccess,
} from "@gocardless/react-dropin";

// Display a button that opens the Dropin on click, starting a checkout
// flow for the specified Billing Request Flow.
const DropinButton = (options: GoCardlessDropinOptions) => {
  const { open } = useGoCardlessDropin({ ...options });

  return (
    <button type="button" onClick={() => open()}>
      Start Dropin for <code>{options.billingRequestFlowID}</code> in{" "}
      <code>{options.environment}</code>
    </button>
  );
};

// Example checkout flow, where we create a Billing Request Flow ID by talking
// with our backend API.
const App: FunctionComponent = () => {
  const [flowID, setFlowID] = useState<string | null>(null);

  // Build your backend with an API endpoint that:
  //
  //   1. Creates a Billing Request for the resources you wish to create
  //   2. Create a Billing Request Flow against (1)
  //   3. Return the ID from (2)
  //
  // See an example of this at Taking an Instant Bank Payment:
  // https://developer.gocardless.com/getting-started/billing-requests/taking-an-instant-bank-payment/
  React.useEffect(() => {
    async function createFlow() {
      // Expecting a JSON body like:
      // {
      //   "flow_id": "BRF123456"
      // }
      let response = await fetch("/api/flows", {
        method: "POST",
      });
      const { flow_id } = await response.json();
      setFlowID(flow_id);
    }
    createFlow();
  }, []);

  // Only show the button once we have a Billing Request Flow ID
  return flowID === null ? (
    <div className="loader"></div>
  ) : (
    <DropinButton billingRequestFlowID={flowID} environment={"live"} />
  );
};

Storybook

Checkout the Storybook flow to see the <GoCardlessDropinButton /> in action. You can use the Storybook knobs to configure the Billing Request Flow ID, from which you can create your Dropin instance.

Stories are deployed to the gh-pages branch of this repo, and hosted at https://gocardless.github.io/react-dropin/.

Publishing

CircleCI is configured to publish changes for us, via a build pipeline.

To trigger a new package version:

  1. Update package.json with the new version number (ie, v1.0.0)
  2. Push this commit to master, then cut a new release in GitHub with a tag name that matches the release (ie, v1.0.0)

This should trigger the CI pipeline, and the new package version will appear on npm shortly.

版本与发布记录

@gocardless/react-dropin 共发布 8 个版本,首次发布于 2021/07/24;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.3.6latest2025/08/261.2 年前
0.3.52023/11/063 年前
0.3.42023/08/153 年前
0.3.22022/08/094 年前
0.3.12022/07/254 年前
0.3.02021/07/295 年前

关于 @gocardless/react-dropin 的常见问题

@gocardless/react-dropin 是什么?

React bindings to the GoCardless Dropin checkout flow

如何安装 @gocardless/react-dropin?

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

@gocardless/react-dropin 支持 TypeScript 吗?

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

@gocardless/react-dropin 支持 ESM 还是 CommonJS?

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

@gocardless/react-dropin 可以商用吗?

@gocardless/react-dropin 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@gocardless/react-dropin 的下载量有多少?

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

@gocardless/react-dropin 还在维护吗?

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