Counter
The “hello world” of Triggery. Two buttons fire an increment/decrement event; a counter component reacts via useAction and re-renders itself. Nothing more.
File layout
Section titled “File layout”- README.md narrative overview
- index.html Vite entry
Directorysrc/
- App.tsx producers + reactors live here
- main.tsx bootstrap
Directorytriggers/
- notification.trigger.ts the rule
The trigger
Section titled “The trigger”src/triggers/counter.trigger.ts
import { createTrigger } from '@triggery/core';
export const counterTrigger = createTrigger<{
events: { increment: void; decrement: void };
actions: { adjust: number };
}>({
id: 'counter',
events: ['increment', 'decrement'],
handler({ event, actions }) {
actions.adjust?.(event.name === 'increment' ? 1 : -1);
},
});The components
Section titled “The components”src/Counter.tsx
import { useAction, useEvent } from '@triggery/react';
import { useState } from 'react';
import { counterTrigger } from './triggers/counter.trigger';
export function Counter() {
const [count, setCount] = useState(0);
const inc = useEvent(counterTrigger, 'increment');
const dec = useEvent(counterTrigger, 'decrement');
useAction(counterTrigger, 'adjust', delta => setCount(c => c + delta));
return (
<div>
<button onClick={() => dec()}>−</button>
<output>{count}</output>
<button onClick={() => inc()}>+</button>
</div>
);
}That’s the whole thing. The producer and reactor happen to live in the same component, but they could be split — the wiring stays the same. See Notification pipeline for a scenario where they really must split.
Related
Section titled “Related” Notification pipeline The same shape, but with three reactors in three components.
Same recipe — Solid With Solid's createSignal.
Same recipe — Vue Composition API version.
Anatomy of a trigger The createTrigger config in full.