4 ms·
My favorite part of redux is how reducers are really just big switch statements: https://redux.js.org/introduction/getting-started#basic-example https://redux.j
by jdxcode 6y ago
My favorite part of redux is how reducers are really just big switch statements: https://redux.js.org/introduction/getting-started#basic-example https://redux.js.org/introduction/getting-started#basic-exam...
- acemarke 6y agoStrictly speaking, nothing says a reducer even _has_ to have a switch statement. A reducer can use _any_ conditional logic it wants inside: `if`, `for`, `while`, lookup tables, etc. It's just that the most obvious conditional construct for "checking multiple possible values of a single field" is a `switch` statement [0]. Having said that, we now officially teach our new Redux Toolkit package as the default way to write Redux code [1], and its `createSlice` API [2] does actually use a lookup table approach instead. On a related note: my next task is rewriting that existing Redux "Basics/Advanced" tutorial to modernize it, and I'm currently taking notes on what I do and don't like about it [3]. If you've got any relevant feedback on things you don't like or ways to improve that tutorial sequence, please feel free to comment and let me know! [0] https://blog.isquaredsoftware.com/2017/05/idiomatic-redux-tao-of-redux-part-2/#switch-statements https://blog.isquaredsoftware.com/2017/05/idiomatic-redux-ta... [1] https://redux.js.org/tutorials/essentials/part-1-overview-concepts https://redux.js.org/tutorials/essentials/part-1-overview-co... [2] https://redux-toolkit.js.org/api/createSlice https://redux-toolkit.js.org/api/createSlice [3] https://github.com/reduxjs/redux/issues/3855 https://github.com/reduxjs/redux/issues/3855