-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make "creating" and "using" a machine seperate #79
Comments
And also that means that users would "need" the second argument of |
I love the idea but I don't think this is super-priority given that most use cases for this library (so far) are for small, in-component state machines. |
Yeah I wouldn't call it "super priority" but it sure makes it impossible for it to be able to use it with React's context as the type isn't available outside the component (and you create the context via |
Just putting this out because I had an idea long ago how to implement this without moving away from React or changing the implementation. It's something like this... const createMachine = (definition) => () => {
// same implementation
}
const useMachine = (definitionOrHook) => {
if (typeof definitionOrHook === "function") {
return definitionOrHook()
}
return createMachine(definitionOrHook)()
} Now the machine (or more precisely the hook to use it) can be created outside the component and hence we can get its type for context too... const useMyMachine = createMachine(...)
type MyMachine = ReturnType<typeof useMyMachine>
const MyMachineContext = React.createContext<MyMachine>()
const SomeComponent = () => {
let machine = useMyMachine()
return <MyMachineContext.Provider value={machine}>
{/* ... */}
</MyMachineContext.Provider>
} |
Would love to see this! |
The fact that
useMachine
“creates” the machine means that we can only create a machine inside an component. This problematic because...createDefinition
solves 1 but doesn't solve 2 so we need something like xstate ielet m = createMachine(d);
then later in componentuseMachine(m)
. We could add an overload to douseMachine(d)
which saves users from writinguseMachine(createMachine(d))
The text was updated successfully, but these errors were encountered: