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


i'm creating app redux , scratching head why best place actions , reducers in separate files. @ least, that's impression i'm getting examples.

each action, or action creator, appears map single function called reducer (inside switch statement). wouldn't logical keep these in same file? makes using same constant action type , switch case easier, doesn't have exported/imported between files.

from redux creator dan abramov:

many reducers may handle 1 action. 1 reducer may handle many actions. putting them negates many benefits of how flux , redux application scale. leads code bloat , unnecessary coupling. lose flexibility of reacting same action different places, , action creators start act “setters”, coupled specific state shape, coupling components well.

from redux docs:

we suggest write independent small reducer functions each responsible updates specific slice of state. call pattern “reducer composition”. given action handled all, some, or none of them. keep components decoupled actual data changes, 1 action may affect different parts of state tree, , there no need component aware of this.

see this conversation on twitter , this issue on github more information.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -