Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redux: Why not put actions and reducer in same file?

I'm creating an app with Redux and am scratching my head as to why it is best to place actions and reducers in separate files. At least, that's the impression I'm getting from all the examples.

Each action, or action creator, appears to map to a single function that is called by a reducer (inside a switch statement). Wouldn't it be logical to keep these together in the same file? It also makes using the same constant for the action type and switch case easier, as it doesn't have to be exported/imported between files.

like image 707
pjivers Avatar asked May 09 '16 08:05

pjivers


People also ask

How actions and reducers are connected in Redux?

An action, is an object that contains the payload of information. They are the only source of information for the Redux store to be updated. Reducers update store based on the value of the action.

What is the difference between actions and reducers Redux?

Reducers: As we already know, actions only tell what to do, but they don't tell how to do, so reducers are the pure functions that take the current state and action and return the new state and tell the store how to do.

How action and reducer are connected?

When you dispatch an action creator it passes the action object to the root reducer. The action object is passed through the entire state tree and any reducers that process the action type consume it.

What happens when you try to dispatch an action within a reducer?

Dispatching an action within a reducer is an anti-pattern. Your reducer should be without side effects, simply digesting the action payload and returning a new state object. Adding listeners and dispatching actions within the reducer can lead to chained actions and other side effects.


2 Answers

From Redux creator Dan Abramov:

Many reducers may handle one action. One reducer may handle many actions. Putting them together negates many benefits of how Flux and Redux application scale. This leads to code bloat and unnecessary coupling. You lose the flexibility of reacting to the same action from different places, and your action creators start to act like “setters”, coupled to a specific state shape, thus coupling the components to it as well.

From the Redux docs:

We suggest you write independent small reducer functions that are each responsible for updates to a specific slice of state. We call this pattern “reducer composition”. A given action could be handled by all, some, or none of them. This keep components decoupled from the actual data changes, as one action may affect different parts of the state tree, and there is no need for the component to be aware of this.

See this conversation on twitter and this issue on github for more information.

like image 74
Shane Cavaliere Avatar answered Oct 21 '22 03:10

Shane Cavaliere


Please DO this if you want. Whoever says anything otherwise does not know what you like and what you don't. People who quote Dan Abramov( just a good developer) into everything are just brainless sheeps.

Now Redux officially released redux-toolkit to encourage saving them in same file. It is much better and you will be able to see one feature into one file instead of many. Now same people will start to praise saving them in same file.

It is a horrible development experience to update types, actions, reducers, operations(thunk/saga) for making a POST request. People who are inherently slow can support such a thing as they don't see the impact.

like image 36
bugwheels94 Avatar answered Oct 21 '22 03:10

bugwheels94