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 Parameters
Section titled “Type Parameters”| Type Parameter | Description |
|---|---|
E extends FactoryMachineEvent<any> | FactoryMachineEvent type |
K extends string | number | symbol | State key type |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
stateKey | K | The state key to match against to.key |
fn | EntryListener<E & { to: FactoryKeyedState<E["machine"]["states"], K>; }> | EntryListener to invoke when matched |
Returns
Section titled “Returns”An entry listener for the specified state key
(
ev:E):void
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
ev | E |
Returns
Section titled “Returns”void
Example
Section titled “Example”setup(machine)( whenState("Active", handleEnterActive),);Source
Section titled “Source”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);