Customer Agent

React hooks

Provider-scoped subscriptions to explicit clients and widget handles.

@customer-agent/sdk/react exports the core factory, Messenger mounting factory, a Provider, and client/widget hooks. Hooks observe the instances supplied by the nearest Provider. They do not use a process-wide or browser-wide Customer Agent singleton.

import { CustomerAgentProvider, useCustomerAgentState } from "@customer-agent/sdk/react"

function Status() {
  const ready = useCustomerAgentState((state) => state.isReady)
  return <span>{ready ? "Ready" : "Starting"}</span>
}

;<CustomerAgentProvider client={client} widget={widget}>
  <Status />
</CustomerAgentProvider>

CustomerAgentProvider

PropTypeRequiredEffect
clientCustomerAgentClientYesSupplies the business runtime.
widgetCustomerAgentWidget | nullNoSupplies an optional mounted Messenger handle.
childrenReactNodeNoComponents that can use the hooks.

The Provider, hooks, and consumer unmounts never call client.boot(), client.shutdown(), or widget.unmount(). The owner of those instances controls their lifetimes. Swapping the Provider's client changes subscriptions without shutting down either client. Separate Providers can observe separate clients. Hooks throw when no Provider exists.

Hooks

HookArgumentsReturns / effect
useCustomerAgentNoneThe supplied CustomerAgentClient; call business methods on it.
useCustomerAgentStateOptional state selectorClient snapshot or selected value.
useCustomerAgentEventClient event name, handlerSubscribes to the client's event bus for the component lifetime.
useCustomerAgentWidgetNoneThe supplied widget handle, or null.
useCustomerAgentWidgetStateOptional UI state selectorWidget snapshot/selected value, or null without a handle.
useCustomerAgentWidgetEventWidget event name, handlerSubscribes to the supplied widget; inactive when none is provided.

useCustomerAgentState reads business state and client readiness. Widget hooks read panel visibility, launcher anchors, and shell readiness. UI controls are called on useCustomerAgentWidget()'s handle; useCustomerAgent() does not expose UI methods.

Selectors can return primitives, objects, or arrays. Results are cached for an unchanged source snapshot and selector so React's snapshot reads remain stable, including under StrictMode. Hooks unsubscribe when their instance changes or the component unmounts. Polling and client messages continue with no React consumers.

Imports, Provider rendering, and client-state hooks support server rendering without a Messenger. Use independent client instances for independent server requests. Server and browser rendering must use matching initial data/options for hydration.

On this page