React State Hook Bindings
React overlays observe state updates and patch adjustments back to the Vault. Rather than subscribing directly to the store, components must use these data-bound hooks to read and update Aircada configurations dynamically.
useAirStore React Hook#
Use the useAirStore hook to read application state configurations and patch values back to the global reactive Vault.
3-Element Tuple Return Signature
The useAirStore<T>() hook returns a 3-element tuple:
const [storeState, patchStore, setProperty] = useAirStore<AirStoreConfig>();storeState: Read-only snapshot of the entire reactive state object.
patchStore(partialState): Merges partial objects into the state hierarchy.
setProperty(key, value): Convenience setter to mutate a single top-level key.
๐ Critical Anti-Patterns & Constraints
- Never Spread State in Patches: Do NOT spread existing state inside a patch (e.g.
patchStore({ ...storeState, open: true })orthis.air.store.patch({ ...this.air.store.state, open: true })). The engine Vault performs atomic deep-partial diffing and automatically preserves all untouched fields. Spreading existing state creates stale closures and can overwrite concurrent updates from 3D operators.
- Never Destructure as an Object: Destructuring
const { store } = useAirStore()fails TypeScript compilation becauseuseAirStorereturns a tuple array.
- Never Treat as useState: Calling
useAirStore('activeStep', 1)assigns the entire store object to your variable and triggers React Error #31 (Objects are not valid as a React child).
- State Configuration Typing: Always provide the generic store config type parameter (e.g.
useAirStore<AirStoreConfig>()) to ensure key autocompletion.
import { useAirStore } from '@aircada/react';
import { AirStoreConfig } from './store/store.config';
export function CartPanel() {
// 1. Signature: useAirStore returns [storeState, patchStore, setProperty]
const [storeState, patchStore, setProperty] = useAirStore<AirStoreConfig>();
const toggleCart = () => {
// ๐ข CORRECT: Deep partial patch containing only the target key
patchStore({ ui: { cartOpen: !storeState.ui.cartOpen } });
};
const handleSelectOption = (finishId: string) => {
// ๐ข CORRECT: Single property setter
setProperty('selectedFinish', finishId);
};
return (
<div className="pointer-events-auto">
<button onClick={toggleCart}>
{storeState.ui.cartOpen ? 'Close Cart' : 'Open Cart'}
</button>
<button onClick={() => handleSelectOption('MATTE_BLACK')}>
Set Matte Black
</button>
</div>
);
}useAirOptions React Hook#
The useAirOptions hook is the standard, data-bound hook for option selectors. It reads configuration selections, loads list details, and applies changes directly through Twin-Proxy paths.
Key Constraints
- Never Call useAirStore for Options: Do not manually subscribe or patch option models using low-level store hooks. Let useAirOptions manage it.
- Pass Option Set Object: Pass the entire
optionSetobject imported from the auto-generatedOptionsRegistry.
import { useAirOptions } from '@aircada/react';
import { OptionsRegistry } from '../registry/options.generated';
export function ColorPicker() {
// Bind to the option set object directly
const { options, isLoading, activeSelection, selectOption } = useAirOptions(
OptionsRegistry.BALL_O_STEEL.sets.SIMPLECOLORS
);
if (isLoading) return <div>Loading...</div>;
return (
<div className="flex gap-2 pointer-events-auto">
{options.map(row => (
<button
key={row.id}
className={activeSelection?.id === row.id ? 'border-cyan-500' : 'border-gray-500'}
onClick={() => selectOption(row)}
>
{row.name}
</button>
))}
</div>
);
}useAirDataset React Hook#
Use the useAirDataset hook when a component only needs to fetch lists of items (e.g. menu contents, layout descriptors) and doesn't need active Option selection tracking.
Key Constraints
- Single Source of Truth: Never hardcode arrays or configurations that could live in a cloud spreadsheet. Maintain them in datasets.
- Strong Typing: Always pass the generic row type parameter to the hook. Import row interfaces directly from
/registry/files.
import { useAirDataset } from '@aircada/react';
import { MediaRegistry, MenuDataRow } from '../registry/media.generated';
export function SidebarMenu() {
// Strongly typed row fetching
const { rows, isLoading } = useAirDataset<MenuDataRow>(
MediaRegistry.DATASETS.MAIN_MENU.id
);
if (isLoading) return <div>Loading...</div>;
return (
<ul>
{rows.map(row => (
<li key={row.id}>{row.Label}</li>
))}
</ul>
);
}useAirEvent React Hook#
Use the useAirEvent hook to trigger transient, fire-and-forget commands (such as resetting camera states, triggering checkout animations, or notifying 3D systems) that do not belong in persistent option states.
Direct Callable Dispatcher Signature
The useAirEvent<TPayload>(eventName) hook accepts the event name string as its first argument and returns a direct callable dispatch function:
const emitEvent = useAirEvent<CartPayload>(EVENT_CART_ADD_REQUESTED);
emitEvent({ productId: 'ring-1', price: 499.00 });
Key Constraints
- Direct Invocation: Call
emitEvent(payload)directly. Do NOT callemitEvent.invoke()or calluseAirEvent()without arguments.
- Strict Payload Typing: Bind the event payload type parameter explicitly to ensure compile-time verification of dispatched parameters.
import { useAirEvent } from '@aircada/react';
import { Events, EventPayloads } from '../registry/registry.generated';
export function ActionButtons() {
// Strongly typed event invocation returning direct dispatch function
const emitResetCamera = useAirEvent<EventPayloads[typeof Events.RESET_CAMERA]>(
Events.RESET_CAMERA
);
const handleReset = () => {
emitResetCamera({ reason: 'User requested reset' });
};
return <button onClick={handleReset} className="pointer-events-auto">Reset Camera</button>;
}