Implementing component state as a combination of booleans may seem like the easiest way to do it, but let's do something different.
Cover by Namro...
For further actions, you may consider blocking this person and/or reporting abuse
State machines are a nice pattern, but
true
andfalse
could also make up the entirety of states of a simple state machine. You don't need to make an enum of them.Can you do a demo so I can refer to the code ?
Thank you sir -
I added a code example in the Exceptions section of the article. :)
I see the author added a part "the exception" and I welcome the addition.
Although there is no possibility to create an algebraic data type in TypeScript, we could artificially simulate it using a combination of classes and type discrimination in order to get a state that is both predictable, warning impossible states and the possibility to embed state in our "types" (that are really classes in a union).
Note that this example uses TypeScript in order to prevent most human mistakes.
In my opinion, having ADT in TypeScript would remove the need for state machines.
Definitely one way to look at it. Sadly it's too verbose in TS.
at this point, you should consider using OOP instead of ADTs to solve this, like move the ifs to the respective classes as methods in order to implement something like the State pattern, making the code less verbose.
Nice write! What's software you use to make that diagram?
Anyway, I also write article about simplifying complex state in React with reducer :)
Simplify state with reducer
M. Akbar Nugroho ・ Mar 8 '23
I'd also like to know the tool to model the state machines.
stately.ai
stately.ai
Very interesting, thanks for writing!
Great it's too help Full Pentagon Detailing...
I love the concept of a "state machine" for its simplicity and ease of understanding.
Thank you !
Good one
Thank you 🙏