loading...

re: State machine advent: A safer way to type events and state (11/24) VIEW POST

TOP OF THREAD FULL DISCUSSION
re: It's a cool idea but keep in mind that you also have to use the values to check the current state or to send events. How would you get the light sw...
 

Another way is to create a plain JavaScript object from an array using .reduce() which allows you to define each string only once.

const LIGHT_SWITCH_EVENT = [
  'TOGGLE',
  'SOME_OTHER_EVENT',
].reduce((obj, item) => {
  obj[item] = item;
  return obj;
}, {});

// then use it like this: LIGHT_SWITCH_EVENT.TOGGLE

I'll stick to enums as I think they are much easier to read.

code of conduct - report abuse