@triggery/zustand
Read a Zustand store from a Triggery condition. Pull-only — the selector runs at fire time, not on every store update, so the host component is never re-rendered because of this hook.
Install
Section titled “Install”pnpm add @triggery/core @triggery/react @triggery/zustand zustand npm install @triggery/core @triggery/react @triggery/zustand zustand yarn add @triggery/core @triggery/react @triggery/zustand zustand bun add @triggery/core @triggery/react @triggery/zustand zustand Peer deps: react >= 18.0.0, zustand ^4 || ^5.
What’s inside
Section titled “What’s inside”| Export | Purpose |
|---|---|
useZustandCondition(trigger, name, store, selector) | Register a Zustand-backed condition. Works with both create (hook) stores and createStore (vanilla) stores. |
Quick example
Section titled “Quick example”import { create } from 'zustand';
import { createTrigger } from '@triggery/core';
import { useZustandCondition } from '@triggery/zustand';
type Settings = { sound: boolean; notifications: boolean };
const useSettings = create<Settings>(() => ({
sound: true,
notifications: true,
}));
const messageTrigger = createTrigger<{
events: { 'new-message': { text: string } };
conditions: { settings: Settings };
actions: { showToast: { body: string } };
}>({
id: 'message-received',
events: ['new-message'],
required: ['settings'],
handler({ event, conditions, actions }) {
if (!conditions.settings.notifications) return;
actions.showToast?.({ body: event.payload.text });
},
});
function SettingsBridge() {
useZustandCondition(messageTrigger, 'settings', useSettings, (s) => s);
return null;
}How it works
Section titled “How it works”The runtime is pull-only: selector(store.getState()) is called only when a trigger fires, not when the store changes. That means:
- Nothing in your React tree re-renders because of this hook. If a component also needs the same slice, call Zustand’s own
useStore(store, selector)alongside. - The trigger always sees the latest state at fire time — no subscription, no possibility of a stale snapshot.
useZustandCondition<T, S, K>(
trigger: Trigger<S>,
name: K,
store: { getState(): T },
selector: (state: T) => ConditionMap<S>[K],
): voidRelated packages
Section titled “Related packages” @triggery/core Exposes Trigger.
@triggery/react useZustandCondition is a React hook.
@triggery/redux Alternative adapter for Redux stores.
@triggery/jotai Alternative adapter for Jotai atoms.
@triggery/mobx Alternative adapter for MobX observables.