Reducing switch statement in Redux reducer

Yini Yin
1 min readMar 2, 2020

We’ve all done it — copy from tutorial, stick it in production code and never look back! So this is what we end up:

Okay, let’s apply some good old Unix programming philosophy — Fold intelligence into data structure so your program can be simple and stupid.

Here is how:

We use a data literal called ‘which’, its keys are action type, properties are a lambda function returns a new state.

By doing this, we fold the correlation (knowledge)between action type and its handler into a data structure so our program can be as stupid as which[action.type].

Thanks for reading if you have any suggestions please PR to my Github, and stay tuned for next article about Dictionary Pattern.

--

--

Yini Yin

Try not to be a jack of all trades always end up being one. Dev@Barin