-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
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
An alternative way to handle actions and reducers #2378
Comments
As far as the file structure goes: totally feel free to do whatever you want. Redux itself doesn't care (per the Redux FAQ entry on file/folder structures ). For the "reducers in actions": This is certainly possible, but as you've already seen in the docs, will break time-travel debugging. It also goes against the overall principles of Redux in that in theory many different reducers should be able to respond to the same action. You may want to read through #155, which included discussion of "cursors" and other similar "specify the path to update"-type approaches. To quote Dan:
So overall... it's entirely possible to write code the way you've suggested. It's not necessarily how you're intended to use Redux. But, ultimately, if it works for you, go ahead. FWIW, I'm actually working on a blog post that will discuss what actual limitations Redux requires and why, vs how you are intended to use Redux, vs how it's possible to use Redux. If you're interested, keep an eye on my blog at http://blog.isquaredsoftware.com . I'm hoping to have it done "soon". |
Thanks @markerikson ! I did see that time travelling and hot reloading would not work as expected with this approach but I was ready to give up some of these features against simplicity and expressivity. What I did not understand well was the reason behind the reducer composition. This part of the FAQ gave me more insights: And particularly the associated discussions:
Maybe the good compromise would be to break actions along use cases and to break reducers along the state structure |
@acailly what if action and reducer were in the same file? This might allow you to still keep it in the same folder. I agree with you about the structure where everything is closer to the component. Mine is something like this:
|
Actually I often have an action which is actionable from multiple components so I don't expect components follow the same structure than actions and reducers. |
Hi redux community,
I would like to have some feedback on a syntax I just started to use and which I find both simpler and more expressive.
But first a disclaimer, I know this is not redux by the book, but I find it respects the core principles enough to worth a discusion 😉
I tested many approaches:
Technical oriented structure 🔨
All actions in an 'actions' folder, all reducers in a 'reducers' folder.
Great to get all actions in one import and pass them to connected component.
Not great when it comes to change a requirement because many files must be changed and they are not in the same place.
Domain oriented structure 🎓
As best explained here, it consists in grouping files by domain concepts instead of technical concepts: https://marmelab.com/blog/2015/12/17/react-directory-structure.html
Great to express what the application is about.
A bit verbose maybe?
Here comes the ducks 🦆🦆🦆
The explanation is here: https://github.com/erikras/ducks-modular-redux
The idea is basically to group together the actions and the reducer in one file.
Great because it's not verbose anymore.
Great because I can see alls concepts of my application in one sight.
But it made me realize one thing: every concept in my application is linked with one reducer.
So my state will be:
Is it right? I'm not sure.
Maybe in a small app with few concepts but when it scales to a big app with multiple related concepts I think this is a constraint we don't want.
Use case oriented structure
Break it differently
What if we break the app along its use cases?
Instead of having
todos.js
andusers.js
we have:And we can group them by concept with folders:
An action embed its own reducer
And what's in
addTodo.js
?As you can see it's an action that embeds its own reducer under the perform attribute.
A more tricky one ? Here's
loadTodos.js
, it usesredux-tunk
middleware:A single reducer
As precised here, http://redux.js.org/docs/recipes/reducers/SplittingReducerLogic.html,
combineReducers()
is not mandatory nor belong to the core principles of redux.So nobody will blame us if we don't use it.
In fact, every reducer will reduce the entire state instead of a substate.
And since the actions embed their own reducers, we really have only one reducer:
Pros
Cons
As mentionned here, http://redux.js.org/docs/recipes/ReducingBoilerplate.html#actions, actions are not serializable due to the perform attribute being a function.
So you can't take a list of serialized action and dispatch them to reproduce a scenario, since the serialized actions don't have the perform attribute.
I personnaly accomodate with that since the time travelling in redux dev tools works well.
WDYT?
Wow! You read all of that, congrats! 🎉
Have I missed an important point?
Have you tried a similar but not quite the same approach ?
Could this be adapted in order to make it more aligned with redux principles?
Should I 🔥 burn 🔥 in hell to have played with sacred concepts?
The text was updated successfully, but these errors were encountered: