-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
take away the huge switch block #883
Comments
Hi, thanks for sharing! Redux isn't opinionated about how you create the reducers, so indeed you can use any convention you like, including this one. There are cases where you want to match by something other than |
@johanneslumpe thanks for recommending this useful package. I like the way it simplify actionCreator and reducer. But I think it isn't necessary to use a reduce method to handle multiple actions. Maybe hash is enough and more efficient. But the little problem can't stop me using it ^ ^ . I'd like to have a discussion with the author and contribute to it. |
@gaearon I'm really exciting for your reply cause I really like redux! Your opinion is right, and I found many packages have already actualized my idea. Even I found the same code in redux docs. haha~ |
@yeatszhang here is another way of avoiding
Advantages are that you can effectively switch on whatever you like, and you can reuse variables defined above the handlers as needed. |
There are a few problems there though: allocating functions on every call and not handling the "unknown action" case. |
@gaearon Good points. I guess |
It's not :-) People tend to obsess over unimportant details, and the disdain for switch seems like one of such cases. |
Another alternative is http://ramdajs.com/0.21.0/docs/#cond |
IMHO switch blocks are really legible and are perfect for documentation where you need to learn how a reducer works;
This looks quite good , only cons is you loose switch case fallback. |
@fracalo your reducer calculates next states for each action in object (no matter for which it was called), this may lead to performance issues. const reducer = (state, action) => ({
[INCREMENT]: state + action.payload,
[DECREMENT]: state - action.payload,
[FOO]: state + action.payload.foo.bar,
}[action.type] || state)
reducer(0, { type: INCREMENT, payload: 1 }) (this will throw "Uncaught TypeError: Cannot read property 'bar' of undefined") |
@Mistereo you're right, once the object is initialized the properties get evaluated so depending on the calculations you need to do it can be way more expensive. You could make methods out of each property: const reducer = (state, action) => {
let o = {
[INCREMENT]: () => state + action.payload,
[DECREMENT]: () => state - action.payload,
[FOO]: () => state + action.payload.foo.bar,
}[action.type]
return o ? o() : state
} but it's starting to get a little noisy. |
@fracalo You can always create a helper, this is the one that I use: createReducer const { createStore, combineReducers } = require('redux')
const { prop, identity, defaultTo, add, subtract, concat, always } = require('ramda')
const createReducer = (actions, INITIAL) =>
(state = INITIAL, { type, payload }) =>
defaultTo(identity, prop(type, actions))(state, payload)
const count = createReducer({ INCREMENT: add, DECREMENT: subtract }, 0)
const list = createReducer({ PUSH: concat, RESET: always([]) }, [])
const { subscribe, dispatch, getState } = createStore(combineReducers({ count, list }))
subscribe(() => console.log(getState()))
dispatch({ type: 'PUSH', payload: [1, 2, 3] })
dispatch({ type: 'DECREMENT', payload: 10 }) |
I really hate the huge switch, so I write a util function to help write reducer in a better way. ( My English is not that good, but I will try to show my idea. )
example
we can write like this.
source code
features
action
andstate
switch
when having a lot of casesbreak
anddefault
The text was updated successfully, but these errors were encountered: