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
| Prop | Type | Required | Effect |
|---|---|---|---|
client | CustomerAgentClient | Yes | Supplies the business runtime. |
widget | CustomerAgentWidget | null | No | Supplies an optional mounted Messenger handle. |
children | ReactNode | No | Components 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
| Hook | Arguments | Returns / effect |
|---|---|---|
useCustomerAgent | None | The supplied CustomerAgentClient; call business methods on it. |
useCustomerAgentState | Optional state selector | Client snapshot or selected value. |
useCustomerAgentEvent | Client event name, handler | Subscribes to the client's event bus for the component lifetime. |
useCustomerAgentWidget | None | The supplied widget handle, or null. |
useCustomerAgentWidgetState | Optional UI state selector | Widget snapshot/selected value, or null without a handle. |
useCustomerAgentWidgetEvent | Widget event name, handler | Subscribes 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.