Like I mentioned earlier, a machine can be represented as a reducer, and that's what this machine.transition function is. You provided the current state and the events that just happened, and you get the next state. What it could also do is provide a sort of events emitter interface in which it contains the state itself and you could send it events and have it manage its own state. This is really useful in situations where you don't want to have to wire up together where to store the state. In addition to that, it's also observable. You could use this with RXJS as well.
It doesn't need to be said, but this is completely framework agnostic. You could use it with any framework. It is set up so that it's easily integrable, whatever the word is. You could use any framework like React, Vue, Angular, Svelte, and more. But speaking of React, there's a lot of useful utilities that allow you to more easily use X state machines with React. So one of these is the use machine hook, and this is just like the use reducer hook. In fact, if you know use reducer, then you basically already know use machine. So instead of passing a reducer, you would pass a machine that you created, and you get the same two expected values from the tuple, the state, which represents the current state of the machine, and send, which represents a way for you to send events to that running machine. Now, the state also has a few utilities, such as matches, so that instead of having to figure out what the exact finite state of the machine is, you could just pass in, like, the expected states or part of the state, like first, second review in this wizard form example, and it provides a nice, clean way to show different parts of the component, and, of course, you could also send events, whether it's just the event string or an entire event object, so it's really useful and really handy, you know, just use it like you would use reducer.
But I do want to talk about some recent and upcoming features to X state and X state React I'm particularly excited about. The first one is use interpret. So use interpret, the goal of this is to interpret a machine, create a service, and it just returns that service. And that service is a single object, which is a reference to, you know, the interpreted version of that machine, which never changes. And so this makes it really useful in context. So if you were to create context with React, now you could pass that service into that context provider and use it wherever you want, such as a wizard. Now, the great thing about this is that since the service doesn't change, it's not going to cause a lot of rerenders. In fact, it only updates once which is when the service is created. So you're guaranteed to never have any extraneous renders. And so the way that you would use this is by a combination of two hooks, use service and use context. So you grab the service from the context, which is that service context, and then using the use service hook, you could use it just like a machine. Instead of passing a machine in, you pass the service in, and you get the two same expected values from the tuple, the current state, and the way to send to that service. And then you could use it as normal. And so this gives you the ability to have both local and global state, and even semi-local state where you need some state shared between a bunch of components, but not with every component. And however, even this, sometimes, even though it does prevent like massive re-rendering due to everything changes, it can also lead to too many renders if one of your components doesn't actually care about a certain part of the state.
Comments