3 ms·
This is the most basic multi-state machine you could make. You could do this with a typescript enum, giving you compile time type checking "for free". I've tri
by beaker52 7y ago
This is the most basic multi-state machine you could make. You could do this with a typescript enum, giving you compile time type checking "for free".
I've tried this "state machine" approach but the problem with designing a React/UI component to respond to a single "state" value like this is that it's not uncommon to end up wanting _more_ states like "has errors and loading", or "has data and loading". At which point you find you're building lots of similar looking subcomponents which get torn apart every time you add a new attribute to the combination. Sometimes it's easier to deal with the combinatorial number of states by passing in the attributes separately and deal with the combinations of state with if/ternary statements, exactly like in the original example.
Plus you don't have to have the switch statement argument with someone for whom not using them was the only thing they remember from Uncle Bob's Clean Code :grin: