@yantrix/react v0.0.2 • Docs
Yantrix API / @yantrix/react / useFSM
Function: useFSM()
ts
function useFSM<Selection>(Automata, options?): TUseFsmReturn
The useFSM
hook is used for initializing and managing an FSM (Finite State Machine) in the context of React.
Type Parameters
• Selection
Parameters
• Automata: TUseFSMProps
<TAutomata
> | TClassConstructor
<TAutomata
>
The automaton class or its props.
• options?: TUseFSMOptions
<TAutomata
, Selection
>
Returns
Throws
If the automaton is not found or its state is undefined.
Description
The hook performs the following actions:
- Initializes the automaton using the provided class or props with a unique automaton ID.
- Provides methods to manage the automaton's state, dispatch actions, and trace its behavior.
- Uses
useSyncExternalStore
to synchronize with the automaton state context and subscribe to its changes.
Example
ts
// Example of using the useFSM hook
const { state, dispatch, getContext } = useFSM(MyAutomataClass);
console.log(state); // Current state of the automaton
dispatch({ type: MyActionType, payload: {} }); // Dispatch an action
const context = getContext(); // Retrieve the current context of the automaton