Why separate actions + reducers in Redux?

I saw an argument for separating actions from reducers because they are many-to-many.

I do not think this is really applicable in Redux. Since there is only 1 data warehouse, the actions for reducers must be 1-to-many.

Typically, reducers are applied to a specific change in a particular data warehouse.

MY_ACTION = "MY_ACTION" function reducer(state, action) { switch(action.type) { case MY_ACTION: // stuff with my action to create new state default: return state } } 

We can combine several reducers with combineReducers , so why not define a handler for the action with the action itself.

for instance

 class Action { constructor(type) { this.type = type this.handlers = [] } add_handler(handler) { this.handlers += handler } get_reducer() { reducer = combineReducers(this.handlers) return (state, action) => { if(action.type == this.type) { return reducer(state, action) } return state } } } 

With the “ducks” pattern, we end up putting the main gears in the same module as the action declaration.

Is there any reason for reducers + actions to be separated with reduction?

+6
source share
1 answer

The main reason for separating the action creators from the reducer function is that the reducer function must be a pure function. If you want to do something in the action creator, for example, as an asynchronous API call, then you cannot put it in a reducer. There is a great explanation here here .

+3
source

All Articles