You are reading the docs for 0.9. The latest version is 0.10. Read this page for 0.10 →

useEvent

function
react/src/hooks/hooks.ts:361
signature
useEvent<EM, C, T>(channel, type, handler, phase, options?): void

Subscribe to store events from a React component. This hook enables reactive UI patterns by allowing components to respond to specific events without selecting state. Useful for: - Showing notifications on certain events - Triggering animations - Logging/analytics - Responding to rejected (uncommitted) events **Phases:** - 'committed' (default): Events that passed middleware and reached reducers - 'uncommitted': Events rejected by middleware - 'all': Both committed and uncommitted events (handler receives phase parameter)

Type parameters

NameTypeDescription
EMextends EventMapBaseEvent map type.
Cextends stringChannel key within EM.
Textends stringEvent type key within channel C.

Parameters

NameTypeDescription
channelCEvent channel to subscribe to.
NameTypeDescription
typeTEvent type to subscribe to.
NameTypeDescription
handler(event: Event<EM, C, T>, getState: () => any, emit: Emit<EM>, phase: NotifiedPhase) => void | Promise<void>Handler called when the event fires. Receives (event, getState, emit, phase).
NameTypeDescription
phaseEventPhaseEvent phase to subscribe to (default: 'committed').

options, object with:

NameTypeDescription
duringReplay?booleanAlso run this handler while devtools is replaying, which it does not by default.

Returns

void

Example

example
useEvent('ui', 'save', (event, getState, emit, phase) => {
  showToast('Saved successfully!');
});

Example

example
useEvent('ui', 'delete', (event, getState, emit, phase) => {
  showToast('Delete was blocked by middleware');
}, 'uncommitted');

Example

example
useEvent('ui', 'action', (event, getState, emit, phase) => {
  console.log('Action:', phase); // 'committed' or 'uncommitted'
}, 'all');