@triggery/zustand
v0.10.0
Published
Zustand adapter for Triggery — `useZustandCondition` hook wiring a Zustand store into a trigger
Maintainers
Readme
@triggery/zustand
Read a Zustand store from a Triggery condition.
Install
pnpm add @triggery/core @triggery/react @triggery/zustand zustandUsage
import { create } from 'zustand';
import { createTrigger } from '@triggery/core';
import { useEvent, useAction } from '@triggery/react';
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
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.
API
useZustandCondition<T, S, K>(
trigger: Trigger<S>,
name: K,
store: { getState(): T },
selector: (state: T) => ConditionMap<S>[K],
): voidWorks with both vanilla stores (createStore) and hook stores (create).
Documentation
Full documentation, recipes and API reference at https://triggeryjs.github.io/packages/zustand/.
Related packages
@triggery/core— Required peer — exposesTrigger<S>.@triggery/react— Required peer —useZustandConditionis a React hook.@triggery/redux— Alternative adapter for Redux stores.@triggery/jotai— Alternative adapter for Jotai atoms.
See the full package list in the repo README.
License
MIT © Aleksey Skhomenko
