Skip to content
GitHubXDiscord

useInlineTrigger

Stable · since 0.1.0

Defines a trigger inline inside a component. The trigger is created on mount, lives until unmount, and is automatically disposed when the component unmounts. Use this for one-off reactions where pulling them into a separate *.trigger.ts file would be overkill — tiny analytics taps, modal-stack coordination scoped to one screen, keyboard shortcuts that only matter while a panel is open.

For anything reusable across the app — prefer createTrigger in a dedicated *.trigger.ts file.

import { useInlineTrigger } from '@triggery/react';
function useInlineTrigger<S extends TriggerSchema>(
  config: UseInlineTriggerConfig<S>,
): void;

type UseInlineTriggerConfig<S> = {
  readonly on: keyof NonNullable<S['events']> & string;
  readonly do: TriggerHandler<S>;
  readonly id?: string;
};
ParamTypeRequiredDescription
config.onkeyof S['events']yesEvent name to react to. Must be stable between renders.
config.doTriggerHandler<S>yesHandler body. Receives the same ctx as a full createTrigger handler.
config.idstringnoOptional debug id. Defaults to an auto-generated stable id (inline:<n>).

void. The trigger is registered in useEffect and disposed on unmount.

import { useInlineTrigger } from '@triggery/react';

function CtaSection() {
  useInlineTrigger<{ events: { 'cta:click': { id: string } } }>({
    on: 'cta:click',
    do: ({ event }) => {
      window.analytics?.track('cta_click', { id: event.payload.id });
    },
  });
  return <button type="button">Sign up</button>;
}
import { useInlineTrigger } from '@triggery/react';

function ShortcutsPanel({ onClose }: { onClose: () => void }) {
  useInlineTrigger<{ events: { 'keyboard:escape': void } }>({
    on: 'keyboard:escape',
    do: () => onClose(),
  });
  return <div>Press Esc to close</div>;
}
import { useInlineTrigger } from '@triggery/react';

function Counter() {
  useInlineTrigger<{ events: { increment: void } }>({
    id: 'counter:tick', // stable across remounts — easier to mock in tests
    on: 'increment',
    do: () => console.log('tick'),
  });
  return null;
}
import { useInlineTrigger } from '@triggery/react';

function ChatRow() {
  useInlineTrigger<{
    events:     { 'message:select': { id: string } };
    conditions: { selectedId: string | null };
    actions:    { highlight: { id: string } };
  }>({
    on: 'message:select',
    do: ({ event, conditions, actions }) => {
      if (event.payload.id === conditions.selectedId) return;
      actions.highlight?.({ id: event.payload.id });
    },
  });
  return null;
}