Introducing a new way to review your PRs.

Read the announcement

chakra-ui / zag

Build your design system in React, Solid, Vue, Svelte or Vanilla. Powered by finite state machines

View on GitHub

19 documents · Updated Sep 21, 2026

docs / react-and-preact-adapters

React and Preact adapters

The React and Preact adapters connect Zag state machines to component rendering. They expose a useMachine service, translate machine-generated DOM props, keep machine effects aligned with component lifetime, and provide common support for refs and portals. See Framework integrations for the wider adapter model, Vue and Solid adapters and Svelte and Vanilla adapters for other bindings, and Overlay and disclosure machines for a common consumer of these bindings.

Adapter surface

Machine services

The useMachine hook turns a machine into a framework service. The service gives a component access to current state, context, computed values, machine refs, the event record, a stable send command, and a scope for DOM work. A scope carries the machine's id information and root-node rules, so overlays and other DOM features can work with a document or shadow root instead of assuming the main document.

The hook accepts machine props as its second argument. The machine can derive its working props from those values, and the adapter keeps the prop reader connected to the latest render. This lets guards, actions, computed values, and other machine code observe changed component props without replacing the service command.

References: packages/…/machine.ts• L37-50 packages/…/machine.ts• L160-188 packages/…/machine.ts• L324-336 packages/…/machine.ts• L32-46 packages/…/machine.ts• L114-128 packages/…/types.ts• L99-118 packages/…/scope.ts• L4-18

Prop normalization

normalizeProps is the boundary between machine prop recipes and JSX or DOM elements. It gives machine code a typed, element-specific prop shape while preserving the target framework's attribute and event conventions. React can pass the normalized props through as-is. Preact maps the event names that differ between the two APIs, such as focus, blur, double-click, and change.

The normalizer excludes ref from its generated prop types. This keeps element refs under the adapter's explicit ref path instead of allowing a machine prop object to silently replace a component ref.

References: packages/…/normalize-props.ts• L4-15 packages/…/normalize-props.ts• L4-33 packages/…/prop-types.ts• L1-83

Reactive rendering

State updates

Machine state and bindable context values are the reactive inputs for a component. A bindable value can be controlled by incoming props or owned by the hook, and a changed value calls the machine's change path so the component can render the new state. State-changing events are flushed with high priority when the machine needs the new state before the surrounding render work continues.

The adapter starts the machine after the component mounts. It queues event delivery and startup work so render stays free of machine side effects. The returned service exposes state accessors rather than a separate view model, which keeps machine transitions and component rendering on the same state source.

References: packages/…/bindable.ts• L9-59 packages/…/machine.ts• L118-158 packages/…/machine.ts• L242-299 packages/…/bindable.ts• L8-58 packages/…/machine.ts• L188-241 packages/…/machine.ts• L264-295

External store bridge

The adapters expose a framework-compatible external-store hook for state that lives outside a component's local hooks. React forwards its native hook, while Preact supplies the same shape through its compatibility layer. This gives framework integrations a shared way to subscribe to external state and read a current snapshot without making the machine service responsible for every store connection.

References: packages/…/index.ts• L1-5 packages/…/index.ts• L1-5 packages/…/use-sync-external-store.ts• L1-9

Stable service handles

Commands and readers returned by useMachine keep stable identities while their behavior follows the latest machine props and state. This allows a component to place service.send or another service member in a dependency list without creating a new subscription or effect on every render. The stable handle still evaluates current guards and props when an event arrives.

References: packages/…/stable.ts• L5-23 packages/…/machine.ts• L118-120 packages/…/machine.ts• L151-158 packages/…/stable-apis.test.ts• L7-60 packages/…/stable-apis.test.ts• L62-106

Lifecycle-safe effects

Machine lifecycle

Machine effects follow state entry and exit. The adapter starts entry work after mount, removes state effects when a state exits, runs machine exit work during unmount, and calls each stored cleanup. This gives subscriptions, timers, listeners, and DOM changes a lifecycle owned by the machine instead of by individual component branches.

React development Strict Mode can mount and clean up a component more than once. The React adapter preserves every cleanup created by those passes, so repeated setup does not leave body locks, layer entries, or other resources behind. The Preact adapter follows the same state-entry and unmount cleanup model through its layout effect.

References: packages/…/machine.ts• L242-281 packages/…/machine.ts• L290-317 packages/…/machine.ts• L188-227 packages/…/machine.ts• L234-256 packages/…/strict-mode.test.tsx• L44-89

Change tracking

Machine code can use track to run an effect after mounted dependencies change. The first render records the dependency values without running the change effect. Later changes run the effect and its returned cleanup, while component unmount resets the tracking lifetime. This is useful for machine behavior that must react to a prop or computed value without treating initial setup as a change.

References: packages/…/track.ts• L3-21 packages/…/track.ts• L1-19 packages/…/types.ts• L31-32 packages/…/types.ts• L99-106

Refs and portals

Machine refs

Machine refs are mutable values owned by the service and exposed through get and set. They hold values that actions and computed logic need to share without making each write a render signal. Element refs remain a component concern and are not included in normalized machine props, which keeps DOM attachment separate from machine bookkeeping.

References: packages/…/refs.ts• L6-16 packages/…/refs.ts• L3-12 packages/…/normalize-props.ts• L4-12 packages/…/normalize-props.ts• L4-12 packages/…/types.ts• L55-58

Portal targets

Portal moves children to a chosen DOM container while keeping them in the component's logical render tree. It uses the supplied container when available and otherwise mounts into the root document body. A machine root-node callback selects the correct owner document for shadow-root or other embedded-document use.

On the server, or when disabled is true, Portal renders children in place. The Preact adapter performs a layout-time refresh so a container ref that becomes available after the first render can be used as the portal target.

References: packages/…/portal.tsx• L7-22 packages/…/portal.tsx• L11-39 packages/…/scope.ts• L4-9

Framework differences

React binding

The React package targets React 18 or later and React DOM. It uses React's layout effect in the browser and falls back to a normal effect when no document exists, which keeps machine startup usable during server rendering. Its portal uses React DOM's portal API, and its public surface also re-exports React's external-store hook.

References: packages/…/package.json• L46-59 packages/…/use-layout-effect.ts• L1-5 packages/…/index.ts• L1-5 packages/…/portal.tsx• L3-22

Preact binding

The Preact package targets Preact 10 or later and uses Preact hooks with the compatibility layer where React-compatible behavior is needed. It exposes an external-store hook through preact/compat and adapts event names in normalizeProps. Its portal also refreshes after layout to resolve a late container ref.

References: packages/…/package.json• L38-49 packages/…/index.ts• L1-5 packages/…/use-sync-external-store.ts• L1-9 packages/…/normalize-props.ts• L15-33 packages/…/portal.tsx• L17-39

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep React and Preact service behavior aligned when changing event delivery, state startup, effect cleanup, or machine status handling.
  • Keep normalizeProps types aligned with the event names that the target renderer accepts. A type-only change can still produce a runtime event mismatch.
  • Preserve stable service member identities when changing prop or state refresh logic. Consumers may use those members in effect dependency lists.
  • Preserve the ref exclusion in normalized props when changing element prop types. Element refs and machine refs have different ownership and lifetimes.
  • Check portal behavior in server rendering, shadow-root documents, disabled mode, and late container-ref resolution when changing portal targets.
  • Keep machine scope root-node handling aligned with portal owner-document selection so DOM queries and portalled content use the same root.

Important failure modes

TriggerConsequenceGuard
A machine sends an event before the adapter marks it startedThe event is ignored and the requested transition does not occurStart the service through the hook lifecycle and treat getStatus as the lifecycle signal when integrating external callers.
A machine effect cleanup is replaced instead of composed during React Strict Mode remountsOne setup can keep a body lock, listener, timer, or layer entry after state exitPreserve all cleanup functions for the same state path. The React adapter's Strict Mode behavior depends on this.
React-style event names are passed directly to PreactFocus, blur, change, or double-click behavior can fail or use the wrong DOM eventRun machine props through the Preact normalizeProps boundary.
A portal resolves the default document instead of the machine rootContent can mount in the wrong document or outside the intended shadow-root contextPass the machine root-node function to Portal and use the intended container when one exists.
A normalized prop object includes a component refMachine-generated props can conflict with the component's element ref ownershipKeep ref outside normalized machine props and attach it through the component layer.

Key files

FileWhy to read it
packages/…/machine.tsPrimary React service lifecycle, event flow, state updates, and effect cleanup.
packages/…/machine.tsPreact counterpart for service behavior and renderer-specific lifecycle choices.
packages/…/bindable.tsReact state bridge for controlled and uncontrolled machine values.
packages/…/bindable.tsPreact state bridge and cleanup hook.
packages/…/normalize-props.tsReact prop contract and ref exclusion.
packages/…/normalize-props.tsPreact event-name translation at the JSX boundary.
packages/…/portal.tsxReact portal target and server fallback behavior.
packages/…/portal.tsxPreact portal target resolution and late-ref refresh.
packages/…/track.tsMounted dependency-change effect behavior.
packages/…/strict-mode.test.tsxGuarantees for effect cleanup under React Strict Mode.
packages/…/stable-apis.test.tsGuarantees for stable service APIs and live prop-driven behavior.
packages/…/types.tsShared contracts for machine services, scope, bindables, refs, and tracked effects.