I would like to put a class as one of the values in my machine context. That way I can easily call methods by retrieving this class from my context. However, the docs say "Ideally, the context should be representable as a plain JavaScript object; i.e., it should be serializable as JSON." What are the cons of putting a class in my context? Am I just modeling my machine incorrectly? For more context, the class I would add to my context is from a third party library, and I am modeling instances of these classes as spawned machines.
#Should I put a class in my context?
1 messages · Page 1 of 1 (latest)
Hi, one downside that I can think of is of possible data loss when persisting context in the localStorage https://xstate.js.org/docs/guides/states.html#persisting-state
What I do when dealing with third-party libraries is to keep the class instance outside of my context and interact with it via actions.
let authConfirmation: FirebaseAuthTypes.ConfirmationResult | null = null;
const authAuthPhoneSignInMachine = createMachine(
/* ... */
actions: {
setAuthConf(context, event) {
authConfirmation = event.data.authConf;
}
}
/* ... */
)
Documentation for XState: State Machines and Statecharts for the Modern Web