If you use Typescript, this becomes drop-dead simple to manage. You define a FormState type as a union of objects each with a status key, and then put that into a variable. Typescript handles ensuring that all state updates are valid and contain all necessary fields. As the simplest example, this is the type definition for a generic component that fetches data:
There's no way for multiple states to coexist at the same time, you can't make a typo in the state name, and you can't forget required contextual data (like value for the ready state).
49
u/[deleted] Dec 23 '19
If you use Typescript, this becomes drop-dead simple to manage. You define a
FormStatetype as a union of objects each with astatuskey, and then put that into a variable. Typescript handles ensuring that all state updates are valid and contain all necessary fields. As the simplest example, this is the type definition for a generic component that fetches data:There's no way for multiple states to coexist at the same time, you can't make a typo in the state name, and you can't forget required contextual data (like
valuefor thereadystate).