Skip to content
GitHubXDiscord

@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.

npm bundle

pnpm add @triggery/core @triggery/react @triggery/zustand zustand

Peer deps: react >= 18.0.0, zustand ^4 || ^5.

ExportPurpose
useZustandCondition(trigger, name, store, selector)Register a Zustand-backed condition. Works with both create (hook) stores and createStore (vanilla) stores.
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;
}

The runtime is pull-only: selector(store.getState()) is called only when a trigger fires, not when the store changes. That means:

  1. 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.
  2. 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],
): void