Skip to content

whenState

whenState<E, K>(stateKey: K, fn: EntryListener<E & { to: FactoryKeyedState<E["machine"]["states"], K>; }>): (ev: E) => void

Defined in: factory-machine-hooks.ts:161

Creates an entry listener that triggers when the event’s to.key matches the given state key.

Type ParameterDescription
E extends FactoryMachineEvent<any>FactoryMachineEvent type
K extends string | number | symbolState key type
ParameterTypeDescription
stateKeyKThe state key to match against to.key
fnEntryListener<E & { to: FactoryKeyedState<E["machine"]["states"], K>; }>EntryListener to invoke when matched

An entry listener for the specified state key

(ev: E): void

ParameterType
evE

void

setup(machine)(
whenState("Active", handleEnterActive),
);

Useful for running logic when entering a specific state in a state machine.

export const whenState = <
E extends FactoryMachineEvent<any>,
K extends keyof E["machine"]["states"],
>(
stateKey: K,
fn: EntryListener<E & { to: FactoryKeyedState<E["machine"]["states"], K> }>
) => when<E>((ev) => ev.to.key === stateKey, fn);