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

Using with React

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?: string) => 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 updates on every React render inside the bundle iframe, but your React app is a separate tree. Use events to get live cart updates.


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.


5. MOB in React

Same pattern — subscribe to elite:mob:ready, then use elite:mob:selection-change for reactive updates.


TypeScript

Import the SDK types from the package if you need them. Since they're not exported as an npm package, copy the relevant interfaces from BYOB SDK Reference and MOB SDK Reference into your own types.ts.

Or add a global declaration to extend the Window type:

Last updated

Was this helpful?