> For the complete documentation index, see [llms.txt](https://docs.loomgate.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.loomgate.io/en/integrations/react.md).

# React

Components and hooks for React 18/19 that work with StrictMode and Next.js.

`@loompay/loomgate-react-sdk` wraps `loomgate.js` as components and hooks for React 18 and 19.

```bash
pnpm add @loompay/loomgate-react-sdk react
```

## Example

```tsx
import { useState } from 'react';
import {
  BrandingElement,
  LoomgatePayment,
  LoomgateProvider,
  PaymentElement,
  loadLoomgate,
  useLoomgatePayment,
  type BillingDetails,
} from '@loompay/loomgate-react-sdk';

// Call loadLoomgate() ONCE, outside render, and only in the browser (pass null on the server).
const loomgatePromise =
  typeof window === 'undefined'
    ? null
    : loadLoomgate('pk_live_…', { apiBaseUrl: 'https://api.loomgate.io' });

export function Checkout({ clientSecret, billing }: { clientSecret: string; billing: BillingDetails }) {
  return (
    <LoomgateProvider loomgate={loomgatePromise}>
      <LoomgatePayment clientSecret={clientSecret}>
        <PaymentElement />
        <PayButton billingDetails={billing} />
        <BrandingElement /> {/* required */}
      </LoomgatePayment>
    </LoomgateProvider>
  );
}

function PayButton({ billingDetails }: { billingDetails: BillingDetails }) {
  const { status, canConfirm, confirm, error, amountTotal, currency } = useLoomgatePayment();
  const [busy, setBusy] = useState(false);
  const [message, setMessage] = useState<string | null>(null);

  if (status === 'error') return <p role="alert">{error?.message}</p>;

  const price =
    amountTotal !== null && currency
      ? new Intl.NumberFormat('en-US', { style: 'currency', currency }).format(amountTotal / 100)
      : null;

  function handleClick() {
    setBusy(true);
    // Don't await anything before confirm().
    confirm({ billingDetails }).then((result) => {
      setBusy(false);
      if (result.error) return setMessage(result.error.message);
      window.location.assign(`/orders/thanks?pi=${result.paymentIntentId}`);
    });
  }

  return (
    <>
      <button type="button" disabled={!canConfirm || busy} onClick={handleClick}>
        {price ? `Pay ${price}` : 'Pay'}
      </button>
      {message && <p role="alert">{message}</p>}
    </>
  );
}
```

## API

| Component                        | Notes                                                                                                                                                                                                                                           |
| -------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `<LoomgateProvider loomgate>`    | `loomgate` is the result of `loadLoomgate()` (a promise or a value), or `null` while it is not loaded. The first non-`null` value is kept: don't call `loadLoomgate()` inline in JSX. After `loadLoomgate()` fails, pass a new promise to retry |
| `<LoomgatePayment clientSecret>` | Owns one session per `clientSecret`. Changing `clientSecret` destroys the old session and creates a new one                                                                                                                                     |
| `<PaymentElement className? />`  | Where the card form is shown                                                                                                                                                                                                                    |
| `<BrandingElement className? />` | The payment partner's branding, **required**                                                                                                                                                                                                    |
| `useLoomgatePayment()`           | `{ status: 'loading' \| 'ready' \| 'error', canConfirm, confirm, error, amountTotal, currency }`                                                                                                                                                |

* `confirm()` works like `session.confirm()` in [loomgate.js](/en/integrations/browser.md): it doesn't throw, and returns `{ status, paymentIntentId }` or `{ error }`.
* `amountTotal` and `currency` are the amount the card form will charge (`null` before the first load). When `confirm()` returns `payment_intent_updated`, `amountTotal` already holds the new amount and `status` is still `'ready'`: let the buyer click again.
* Works correctly under `<StrictMode>`: after mount → unmount → mount there is only one card form.

## Next.js and SSR

The package already includes `"use client"`, so it can be imported from a Server Component. The components are still rendered on the server, so only call `loadLoomgate()` in the browser (as in the example above) and pass `null` when rendering on the server. Create the payment (to get the `clientSecret`) in a Route Handler or a Server Action with the [server SDK](/en/integrations/node-server.md).

## Full example

* [`examples/react`](https://github.com/loomgroup/loomgate-examples/tree/main/examples/react) in the [loomgate-examples repository](https://github.com/loomgroup/loomgate-examples): a complete checkout page built with the React SDK and shadcn/ui, with the server part that creates the payment.
* The [playground](https://examples.loomgate.io) is also built with the React SDK: run each step (fee quote, create a payment, pay, refund) with your own merchant keys and see the code of each step. Payments in the playground use real money, and fees are not refunded.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.loomgate.io/en/integrations/react.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
