You can make your code more visualizable and that helps you. But what are we doing with planning, right? We are mostly following the concept of implementation sourced planning, right? And not maintainability source planning. So with that example previously that we showed, we are mostly thinking about, okay, what is there on our effects? What are the props? What are the states and whatnot? And that actually brings us to a conclusion that what can be a better way, right? What can be a better way of planning our UIs rather than planning implementation.
And now you might be able to see that since we have a lot of states and there can be a lot of permutations and combinations of those states in the future, there has to be a way, or there has to be some sort of a system where we can be saved from this multiple if else statements to handle what to do when this state is there and this state is not there. There might be a system where we can just check and we can just orchestrate our states and transitions between those states in a better way. And obviously you guessed it, right. So there comes finite state machines to our rescue.
So if I had to put this in a very simpler way what finite state machines have to offer is basically you have an initial state or initial condition or an initial situation of the UI, which it might be in. And then you might perform some action with that state. And like if that state receives an action, it goes on to a next state. And at every point of time, each state knows based on an action that what is gonna be the next state that UI or your code is gonna go through. And that's what these three words mean, right? You have a finite number of states. You have a state obviously, which is a condition or the situation your UI is in. And basically there's a machine, which is orchestrating that state and how those states transition based on actions. So that is mostly the whole point of a finite state machine. All right, so that being said, let's try to start thinking about how we can think in state machines with that.
All right, so with that example in mind, we are mostly going to see that, okay, there are basically four types with which a UI can exist. Either it can be idle or I might click the button to make my UI go in a loading state that, okay, I'm being logged in. And if I logged in successfully, I go to an isSuccess state or I go to an isError state. And that's what makes our transitions defined. We define that, okay, what is the current state and based on that what are the transitions that that state can go to and like what's that next state. And basically that being said, you have a state, you have an action. For example, if I'm in an idle state, I do an action called submitForm and then I go to a state called isLoading. And that's how you visualize things and how you create a mental model of state machines in your mind.
So, if you see a mapping between your visual and code, it's just basically on the right side that you see two simple objects where a state subject just defines what are these circles on the left, which are the states that you have. And this transitions object just lets you know that, okay, if I am on a state, let's say, states.idle, and I perform an action called submitForm, I go to a state called states.loading. So that makes your UI more predictable and like there's this logic all at one place. And obviously, since we have this brain ready, we have to tell this brain how to communicate between its other states, right? So, this is a function called transition. And what it does is like, basically, it knows that, okay, what's the current state? What's the action? And based on this, where do I have to go to the next state? And this brain and the body being set up this machine, you just have a simple function just down below, which is updateForm state, where you just do nothing but just let transition tell you that, okay, what's the next state? And I just set my state based on that.
All right, so, enough being said, I think you're bored of all this theories.
Comments