# Examples that connect to your checkout

> STEP 4-2

These examples include parts that depend on your checkout handling. Your code won't necessarily take this shape, so adapt them before you use them. As long as you follow the rules on the step pages, your code doesn't need to match the examples.

## `components/CheckoutButton.tsx`

An example of the checkout button. Pass your function that starts checkout as `getCheckoutUrl`. Match the look of the button and the conditions that disable it (`disabled`) to yours. The rules are in [Step 5: Replace the checkout button](./shopify-headless-checkout-button.html).

```tsx
// `components/CheckoutButton.tsx` (React 19 or later example)
'use client'
import { useEffect, useSyncExternalStore, type ReactNode } from 'react'
import { useCheckoutPlus } from '@/components/CheckoutPlusProvider'

// Whether checkout is being prepared. Shared by every checkout button and the cart line list on the page
let checkingOut = false
const listeners = new Set<() => void>()
function setCheckingOut(value: boolean): void {
  checkingOut = value
  for (const listener of listeners) listener()
}
function subscribe(listener: () => void): () => void {
  listeners.add(listener)
  return () => {
    listeners.delete(listener)
  }
}

/** `true` while checkout is being prepared. Use it to stop quantity changes and removals in the cart line list */
export function useCheckingOut(): boolean {
  return useSyncExternalStore(subscribe, () => checkingOut, () => false)
}

/**
 * The checkout button. After Checkout+ is ready, it navigates to the current `checkoutUrl`
 *
 * Replace the links to checkout on the cart page and in the cart drawer
 * with `<CheckoutButton getCheckoutUrl={...} />`.
 * `getCheckoutUrl` is your normal checkout start.
 * It gets and returns the current cart's `checkoutUrl`.
 * If it can't, your normal code shows the error and it returns `null`
 */
export function CheckoutButton({
  getCheckoutUrl,
  disabled = false,
  children = 'Proceed to checkout',
}: {
  getCheckoutUrl: () => Promise<string | null>
  // `true` when your normal condition for checkout isn't met (for example, while the cart is being updated)
  disabled?: boolean
  // The button text. Defaults to "Proceed to checkout"
  children?: ReactNode
}) {
  const checkoutPlus = useCheckoutPlus()
  const pending = useCheckingOut()

  // Restore when the browser's Back button shows the previous screen as is
  useEffect(() => {
    const onPageShow = (event: PageTransitionEvent) => {
      if (event.persisted) setCheckingOut(false)
    }
    window.addEventListener('pageshow', onPageShow)
    return () => window.removeEventListener('pageshow', onPageShow)
  }, [])

  async function handleClick() {
    if (checkingOut || disabled) return // Preparation starts only once, whichever checkout button is pressed
    setCheckingOut(true)
    // Never throws, and always completes. If there's no SDK, it proceeds without doing anything
    await checkoutPlus?.prepareCheckout()
    let navigating = false
    try {
      const url = await getCheckoutUrl()
      if (url !== null) {
        window.location.assign(url)
        navigating = true
      }
    } finally {
      if (!navigating) setCheckingOut(false) // If navigation couldn't start, restore so the shopper can press it again
    }
  }

  return (
    <button type="button" disabled={pending || disabled} onClick={handleClick}>
      {children}
    </button>
  )
}
```

From when the checkout button is pressed until navigation to checkout starts, `useCheckingOut()` is `true`. You can also use it to stop the quantity and remove buttons in the cart line list. However many checkout buttons there are, preparation starts only once ([Step 5: Replace the checkout button](./shopify-headless-checkout-button.html)).
