useCondition
Stable · since 0.1.0
Registers a pull-only condition getter on a trigger. The runtime calls getter() only at fire time — there is no subscription, and the component does not re-render when the trigger fires.
deps works like useMemo: when any element changes, the runtime sees the updated closure.
Import
Section titled “Import”import { useCondition } from '@triggery/react';
Signature
Section titled “Signature”function useCondition<S extends TriggerSchema, K extends ConditionKey<S>>(
trigger: Trigger<S>,
name: K,
getter: () => ConditionMap<S>[K],
deps?: readonly unknown[],
): void;Parameters
Section titled “Parameters”| Param | Type | Description |
|---|---|---|
trigger | Trigger<S> | The trigger returned by createTrigger. |
name | K extends ConditionKey<S> | A condition declared in the trigger’s schema. |
getter | () => ConditionMap<S>[K] | A synchronous function returning the current value. |
deps | readonly unknown[] (default []) | Same semantics as useMemo — when any element changes, the captured closure is refreshed. |
Returns
Section titled “Returns”void. Registration is performed in useEffect and cleaned up on unmount.
Examples
Section titled “Examples”Basic provider
Section titled “Basic provider”import { createTrigger } from '@triggery/core';
import { useCondition } from '@triggery/react';
import { useState } from 'react';
const trigger = createTrigger<{
events: { ping: void };
conditions: { user: { id: string; name: string } | null };
}>({
id: 'demo',
events: ['ping'],
required: ['user'],
handler({ conditions }) {
if (!conditions.user) return;
console.log('user', conditions.user.name);
},
});
function UserProvider() {
const [user, setUser] = useState<{ id: string; name: string } | null>(null);
useCondition(trigger, 'user', () => user, [user]);
return null;
}Without deps (a stable getter)
Section titled “Without deps (a stable getter)”If your getter reads from a ref or a module-level value, deps is unnecessary:
import { useCondition } from '@triggery/react';
import { trigger } from './x.trigger';
const settingsRef = { current: { sound: true } };
function StableProvider() {
useCondition(trigger, 'settings', () => settingsRef.current);
}From an external store (Zustand example)
Section titled “From an external store (Zustand example)”import { useCondition } from '@triggery/react';
import { useStore } from 'zustand';
import { settingsStore } from './stores/settings';
import { trigger } from './x.trigger';
function Provider() {
const settings = useStore(settingsStore);
useCondition(trigger, 'settings', () => settings, [settings]);
}For a zero-rerender adapter that bypasses the React state path, use @triggery/zustand.
Related
Section titled “Related” useEvent Fire events from producers.
useAction Register an action handler.
Conditions guide The full narrative on pull-only, snapshots and ownership.
@triggery/zustand Use a Zustand store as a condition without re-rendering.