> Open Plandalf checkouts from a JSX attribute with the hosted script or from a React click handler with the React package.

Source: https://plandalf.com/docs/frameworks/react

Frameworks

# React

Use the hosted script to open a checkout from a JSX attribute in Vite or Create React App. To open one from a hook, use the [React package](#the-plandalf-react-package).

## Add the script

1.  ### Install the SDK + use data attributes
    
    The SDK auto-discovers any element with the canonical data attributes — even ones React renders later.
    
    ```tsx
    // public/index.html — load the SDK once, before </body>:
    //   <script async src="https://YOUR-ORG.plandalf.dev/js/plandalf-sdk.js"></script>
    
    // Then use the data attributes anywhere in your tree.
    export function BuyButton() {
      return (
        <button
          data-plandalf-present="YOUR-CHECKOUT"
        >
          Buy Now
        </button>
      );
    }
    ```
    
2.  ### Make the first open instant (optional)
    
    Paste this under the SDK script tag on the pages that hold the trigger. It fetches the checkout UI (about 1.2 MB) in the background while the page sits idle, so the click only pays for creating the session. Leave it off pages with no checkout: visitors download it whether or not they buy. In a React app, call sdk.preload('checkout') from the ready callback in an effect instead.
    
    ```html
    <script>
      // Optional: fetch the checkout UI while the page is idle, so the first click opens at once.
      window.plandalf = window.plandalf || function () { (window.plandalf.q = window.plandalf.q || []).push(arguments); };
      plandalf('ready', (sdk) => sdk.preload('checkout'));
    </script>
    ```
    

**Tip**

For programmatic control: plandalf.present('YOUR-CHECKOUT') returns a FlowHandle (a Promise, plus .events and .close()).

In a Vite app, the HTML file is `index.html` at the project root; in Create React App it's `public/index.html`. Paste the script before `</body>`:

```html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>My app</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.tsx"></script>
    <script async src="https://YOUR-ORG.plandalf.dev/js/plandalf-sdk.js"></script>
  </body>
</html>
```

Keep it a plain script, not `type="module"`, and don't import it from your bundle.

## Open a checkout from a button

JSX passes `data-*` attributes straight through, so the attribute works on any element:

```tsx
export function BuyButton() {
  return <button data-plandalf-present="YOUR-CHECKOUT">Buy Now</button>;
}
```

### From your own click handler

This helper queues calls made before the script loads, so you can call it at any time:

```ts
type Plandalf = ((...args: unknown[]) => void) & { q?: unknown[][] };

declare global {
  interface Window {
    plandalf?: Plandalf;
  }
}

export function plandalf(...args: unknown[]): void {
  if (typeof window === "undefined") return; // nothing to do on the server
  // Hold calls made before the script loads; it runs them when it arrives.
  window.plandalf ??= function (...a: unknown[]) {
    (window.plandalf!.q ??= []).push(a);
  } as Plandalf;
  window.plandalf(...args);
}
```

```tsx
import { plandalf } from "./lib/plandalf";

export function UpgradeButton() {
  return <button onClick={() => plandalf("present", "YOUR-CHECKOUT")}>Upgrade</button>;
}
```

To wait for the result, call `plandalf("ready", callback)`: the callback gets the SDK, and its `present()` returns a handle you can await. See [Open a checkout from code](https://plandalf.com/docs/developers/open-a-checkout).

## Show a checkout on the page

Render an empty element with `data-plandalf-mount`. The script finds it when React adds it to the page:

```tsx
export function Checkout() {
  return <div data-plandalf-mount="YOUR-CHECKOUT" style={{ minHeight: 480 }} />;
}
```

## Show a deadline timer

Render an empty element with your deadline timer's slug. The script draws the live countdown inside it. To charge the deadline timer's current price, add `data-plandalf-apply-promo` with the same slug to your buy button.

```tsx
<div data-plandalf-promo="YOUR-PROMO" />
<button data-plandalf-present="YOUR-CHECKOUT" data-plandalf-apply-promo="YOUR-PROMO">Buy Now</button>
```

See [Countdowns](https://plandalf.com/docs/promos/countdowns).

## The @plandalf/react package

To open checkouts with React hooks, install the package and wrap your app in `<PlandalfProvider>`. Set `apiBase` to your organization's Plandalf address.

```bash
npm install @plandalf/react
```

```tsx
import { createRoot } from "react-dom/client";
import { PlandalfProvider } from "@plandalf/react";
import App from "./App";

createRoot(document.getElementById("root")!).render(
  <PlandalfProvider apiBase="https://YOUR-ORG.plandalf.dev">
    <App />
  </PlandalfProvider>,
);
```

In a child component, take `const { present } = usePresent()` and call `present('YOUR-CHECKOUT')` from your click handler. See [React components and hooks](https://plandalf.com/docs/developers/react) for the full button example.

## Check it's working

1.  ### Run the app
    
    If you used the hosted script, type `typeof window.plandalf` in the browser console. Look for `"function"`.
    
2.  ### Click your button
    
    Plandalf should display your checkout over the page.
    
3.  ### Make a test purchase
    
    Add `localhost` and your preview domain in Plandalf under **Settings → Advanced → Test domains**. Checkouts there use Stripe test cards. See [Test mode](https://plandalf.com/docs/get-started/test-mode).
    

## If it isn't working

-   Calling `window.plandalf.present()` straight away can run before the script has loaded. Use the helper above, which queues the call.
-   If your app sends a Content Security Policy, allow `https://*.plandalf.dev` and `https://js.stripe.com` in `script-src`, `connect-src` and `frame-src`.
-   Add `data-debug="true"` to the script tag and the console logs each step the script takes.

See [Troubleshooting](https://plandalf.com/docs/help/troubleshooting).

Something wrong or missing? Email [hello@plandalf.com](mailto:hello@plandalf.com).
