GitHub - cassiozen/useStateMachine: The ½ kb state mach

GitHub - cassiozen/useStateMachine: The ½ kb state mach

GitHub - cassiozen/useStateMachine: The ½ kb state machine hook for React


Async orchestration (Fetch data with limited retry) - CodeSandbox - Source
Installation
Sample Usage
const [state, send] = useStateMachine()({
initial: inactive ,
states: {
inactive: {
on: { TOGGLE: active },
},
active: {
on: { TOGGLE: inactive },
effect() {
console.log( Just entered the Active state );
// Same cleanup pattern as `useEffect`:
// If you return a function, it will run when exiting the state.
return () => console.log( Just Left the Active state );
},
},
},
});
console.log(state); // { value: inactive , nextEvents: [ TOGGLE ] }
send( TOGGLE );
// Logs: Just entered the Active state
console.log(state); // { value: active , nextEvents: [ TOGGLE ] }
What s up with the double parenthesis?
useStateMachine is a curried function (Yummm tasty!) because TypeScript doesn t yet support partial generics type inference.

Related Keywords

, Extended State, Ijust Left, Optional Context, State Machine, நீட்டிக்கப்பட்டது நிலை, வெறும் இடது, நிலை இயந்திரம்,