-
Notifications
You must be signed in to change notification settings - Fork 46
Description
Hey, first of all thanks for this project, it's great to see finite state machines gaining traction in react development and i'm a big advocate of them myself!
I'm interested whether this library has plans for offering a hook (e.g. useStateMachine
or similar)? I'm interested in creating a solution myself but am not sure if it would be suitable to be included as part of this project, or as a separate package.
My rough idea is to have a useStateMachine(statechart)
hook where statechart
is an object which is then interpreted by xstate. This would return [State, transition]
where State
is a component (with is
prop that can be a string, array of strings/glob pattern) and transition
is a function that executes an action on the current state of the machine (e.g. transition('EVENT')
). An 'optional' third value, machineState
would also be returned, if the user wishes to tightly couple their UI with the internal machine's state.
Example usage would be similar to:
function App() {
const statechart = { ... }
const [State, transition, machineState] = useStateMachine(statechart);
return (
<button onClick={() => transition('TOGGLE')}>toggle</button>
<State is="active">active</State>
{ machineState === 'inactive' && <span>inactive</span> }
)
}
State
could maybe accept children as a pure React node or as a render prop function with visible
(or similar) passed as a parameter.