Skip to main content

React Hooks for App Bridge

@launchmystore/app-bridge-react ships a typed hook for every App Bridge action family. Each hook wraps the underlying SDK action class from @launchmystore/app-bridge/actions, so the wire protocol stays exactly the same — you just don’t have to construct messages by hand.

Installation

AppBridgeProvider

Mount once near the root of your iframe app:
AppBridgeProvider takes a single config prop ({ apiKey, host }). The earlier docs showed apiKey / host as separate props — that form is not supported.
The provider creates one App instance, tears it down on unmount, and hands it to every hook through a React context. Calling any hook outside the provider throws useAppBridge must be used within an AppBridgeProvider.

Hook Index

Core

useAppBridge

Access the raw App instance — useful when a built-in hook doesn’t cover your case or you want to call app.dispatch(action, payload) directly.

Authentication

useSessionToken

Manages a cached JWT for authenticated backend calls. The hook fetches one token on mount and exposes getToken() (returns the cached token unless it’s near expiry, then refreshes) plus refresh() (forces a new token).
Return shape:

useAuthenticatedFetch, useAppQuery, useAppMutation

Helpers built on top of useSessionToken. useAuthenticatedFetch() returns a wrapped fetch that injects the Bearer header automatically:
See Sessions & Authentication for the data- fetching variants (useAppQuery, useAppMutation).

UI Surface

useToast

Returns an object with show, success, error, warning, info — not a callable. Toast options accept { message, duration?, type? }.
The admin host reads payload.type for the toast variant. The React checkout host reads payload.variant. The useToast hook always sends type — if you’re posting toasts from a checkout iframe, build the payload manually with app.dispatch('TOAST_SHOW', { message, variant }).

useModal

Opens a non-promise modal whose button callbacks fire as you set them.
Returns { open(): void; close(): void }. Each open() call creates and dispatches a fresh modal instance; close() dismisses the last one.

useConfirmationModal

Promise-returning variant — perfect for inline await.

useTitleBar

Configures the host’s title bar. Returns update() (merges new options into the live title bar) and setPrimaryLoading() for the common case of showing a spinner on the primary button during async work.

useNavigationMenu

Declares the menu rendered by the admin chrome when your app is mounted. Provide an items array; the host renders them next to the page title.

useContextualSaveBar / useDirtyState

useContextualSaveBar is the low-level hook — call show() / hide() / update() / setSaveLoading() / setDiscardLoading() directly.
useDirtyState glues that together with an internal dirty flag:

useLoading

Drives the host’s global thin-progress-bar via the LOADING_START / LOADING_STOP actions. Use wrap() for the common pattern.
Returns { start, stop, wrap<T>(fn: () => Promise<T>): Promise<T> }.

useFullscreen

isFullscreen flips to true when the host posts back FULLSCREEN_ENTERED (similarly false on FULLSCREEN_EXITED), so you can mirror the host’s actual state instead of guessing.

useLeaveConfirmation / useUnsavedChanges

useLeaveConfirmation exposes manual enable() / disable() / setMessage(). useUnsavedChanges wraps it with a setDirty(boolean) helper that enables/disables in lock-step with form state.
Both hooks also wire the browser’s native beforeunload event under the hood, so closing the tab triggers the same confirmation.

Resource Picker

useResourcePicker

Generic resource picker — pass resourceType plus optional multiple, initialSelectionIds, filter, onSelect, onCancel.
Returns { open(): void; close(): void }.

Typed convenience hooks

Each takes the same options as useResourcePicker minus resourceType.
There is no useOrderPicker / useArticlePicker / useMenuPicker. For those, call the generic useResourcePicker({ resourceType: 'order' }). The full type list lives in Actions Reference.

useRedirect

Returns a menu of typed navigation helpers — the generic navigate() plus open() (new tab) and one helper per Admin resource detail page.

Browser APIs

useClipboard

copy() writes through the browser’s navigator.clipboard.writeText API. paste() round-trips to the host (CLIPBOARD_READ_REQUEST) — required because Chrome blocks clipboard.readText() in cross-origin iframes.
useCopyToClipboard() is the slim variant — just (text) => Promise<void>.

usePrint

usePrint() returns a single function: print() => void. The host opens the browser print dialog scoped to your iframe.

useShare

Wraps the Web Share API (mobile). useShare() returns { share: (data: { title, url, text? }) => Promise<void>; isSupported }.

useHistory

Drives the host browser history — push, replace, go, back, forward. Useful when your app uses its own router and you want host-aware history.

Lifecycle

useLifecycle({ onFocus, onBlur, onVisible, onHidden }) plus the four single-event variants useOnFocus, useOnBlur, useOnVisible, useOnHidden. Fired when the host detects the iframe has focused, blurred, become visible, or been hidden.

Data APIs

All four are RPCs the first time you call them and cache the result; refresh() forces a round-trip.

Subscriptions

Three escape hatches when no typed hook exists for what you need:
  • useAppSubscription(action, callback) — subscribe to a host-initiated action (e.g. MODAL_PRIMARY_ACTION). Returns nothing; cleanup happens on unmount.
  • useAppDispatch() — returns the bound (action, payload?) => string function from the underlying App.
  • useAppDispatchAndWait() — returns the bound (action, payload?) => Promise<payload> function.

Checkout

useCart

Returns a memoised Cart action instance for the checkout host. Use it inside checkout extensions to mutate the host cart:
See App Bridge for Checkout for the full payload shapes (applyDiscountCodeChange, applyNoteChange, applyAttributeChange, applyGiftCardChange, etc.).

Complete Example

A realistic admin block that combines auth, save bar, resource picker, title bar, and the loading indicator:

TypeScript

Every hook ships with typed return values, options, and accompanying types — UseToastReturn, UseModalOptions, UseResourcePickerReturn, UseSessionTokenReturn, etc. Import from the package root.

useI18n

Subscribes to host-driven locale changes and exposes translate, formatNumber, formatDate helpers (backed by Intl.*).
The hook re-renders whenever the host pushes a new I18N_UPDATE, so locale changes from the admin chrome propagate without manual handling.

useRestApi

Returns authenticated fetch / fetchJson helpers for the LaunchMyStore REST API. Session token and API base URL are resolved lazily and cached.
Use REST verbs (GET / POST / PUT / PATCH / DELETE) against /api/v1/... paths. The hook handles Authorization: Bearer <token> and resolves the base URL from CONFIG_GET so your iframe works in dev, staging, and production.

useIntents

Launches another app’s admin action by intent target.

useApi — the umbrella hook

For pages that need most of the App Bridge surface, useApi() returns a single object containing every helper:
This is the recommended entry point for admin block / admin action React components — one shared API surface, automatic listener cleanup, and stable references across renders.

See Also