For the complete documentation index, see llms.txt. This page is also available as Markdown.

Using with React

Patterns for consuming the Elite Bundle Builder SDK from a React app embedded in a Shopify theme: hooks, events and cart actions.

The SDK is framework-agnostic — it exposes plain JavaScript objects and events. This guide shows patterns for consuming the SDK inside a React app embedded in a Shopify theme.


Setup

Install your React app however you like (a <script type="module"> tag, Vite, or a bundled file). It just needs to run on the same page as the bundle.

The SDK is always available at window.eliteBundle — no install or import required. You access it from React via refs and effects, the same as any other global.


1. Wait for ready and sync to state

Use a useEffect to subscribe to the ready event, then mirror the SDK state into React.

import { useState, useEffect, useCallback } from 'react';

interface ByobSDK {
  steps: any[];
  cart: any[];
  cartTotalFormatted: string;
  canCheckout: boolean;
  addToCart: (product: any, variantId: string, stepId?: string) => void;
  checkout: (target?: "stay_on_page" | "cart" | "checkout") => Promise<void>;
  formatPrice: (amount: number) => string;
}

function useBundleSDK() {
  const [sdk, setSdk] = useState<ByobSDK | null>(null);
  const [isReady, setIsReady] = useState(false);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    if (!window.eliteBundle?.sdk) return; // SDK not enabled

    function onReady() {
      setSdk(window.eliteBundle.sdk.byob);
      setIsReady(true);
    }

    function onError(e: CustomEvent<{ error: string }>) {
      setError(e.detail.error);
    }

    document.addEventListener('elite:byob:ready', onReady);
    document.addEventListener('elite:byob:error', onError as EventListener);

    // Handle ready already fired
    const current = window.eliteBundle.sdk.byob;
    if (current && !current.isLoading && !current.error) onReady();

    return () => {
      document.removeEventListener('elite:byob:ready', onReady);
      document.removeEventListener('elite:byob:error', onError as EventListener);
    };
  }, []);

  return { sdk, isReady, error };
}

2. Sync cart state from SDK events

The SDK state lives outside your React tree, and the SDK object is replaced on every update — so a value you put in state goes stale. Subscribe to events for live updates, and read window.eliteBundle.sdk.byob fresh when you act on it.


3. Example component


4. Per-card quantity state

Instead of storing quantities in React state (which would require diffing the cart array on every change), read them directly from the SDK on demand.


TypeScript

The SDK is not published as an npm package, so copy the interfaces you need from the Bundle SDK Reference into your own types.ts.

Or add a global declaration to extend the Window type:

Last updated

Was this helpful?