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
Action-Creators and Reducers: Business Logic #602
Comments
That's quite a bit of information 😄
|
Thanks for doing some research!
Yup, I agree.
Yes, it is in alignment with being more functional and declarative. I need to spend more time with the generator pattern and see if I don't like it because it's unfamiliar or because my intuition is right and there are better ways. I think that's why I was attracted to some of the frp solutions.
Do you think @anmolarora1 I didn't see an explicit comment about fat reducers. Do you think that decoupling side effects and moving as much logic to reducers as possible is a reasonable approach? That's where I'm leaning currently. |
@raineorshine My thoughts on the topic -
|
Yes, this makes a lot of sense. It leaves me wondering:
In a typical web app, I could imagine different slices of the state corresponding to different domains of the app: Customers, Orders, Account Information, etc. 90% of em is effectively one component: the editor and its corresponding thoughts. Because of this, I would venture to guess that em has higher than typical cross-state dependencies. In this case, leveraging more reducer composition will yield more gains than trying to partition the state. I can only speculate though, as I have not built other React apps. There are parts of the state like the
Yes, an open question. |
@raineorshine I'm not sure about having fat reducers, perhaps because of my aversion towards fat functions in general. But we don't want fat action-creators. |
I'm more "anti fat action-creators" than "pro fat reducers" too 😁
That sounds like a good plan: iterative and purposeful. |
Almost all synchronous, pure action-creators have been converted to reducers in #706. |
Should we have more business logic in action-creators or reducers?
This is mostly a response to “Recommendations for best practices regarding action-creators, reducers, and selectors”:
reduxjs/redux#1171.
This thread is 5 years old, so a newer source should probably be consulted before making a final conclusion. Nevertheless I read the whole thread and the consensus seemed to be that action-reducers should be small, simply representing the user or browser action, and the logic should go into the reducers. The original post that recommended fat action-creators was speculation from someone who had been using Redux for only a couple months.
Reducers are easier to test and compose since they are pure functions. Plus you get the benefit of things like hot reloading when the logic is in reducers. There was a fair amount of pragmatism represented in true Redux fashion, but the recommend approach was in favor of logic in reducers.
Here’s my evidence:
Dan Abramov:
reduxjs/redux#1171 (comment)
Imperative vs Reactive: reduxjs/redux#1171 (comment)
“Action creators are just a pattern for organizing the code. It’s convenient to have factories for actions because you want to make sure that actions of the same type have consistent structure, have the same side effect before they are dispatched, etc. But from Redux point of view, action creators don’t exist—Redux only sees actions.”
Hot reloading benefit: reduxjs/redux#1171 (comment)
A comment Dan made also suggested that composing reducers is generally a good thing: reduxjs/redux#1171 (comment)
“For reducers, we also don’t recommend using classes because it will be harder to use reducer composition, that is, reducers that call other reducers.” [emphasis mine]
More:
reduxjs/redux#1171 (comment)
reduxjs/redux#1171 (comment)
Other considerations:
sync
out of the reducers, although I’m also consider wrapping a reducer so that it can be called just with the state delta (change in state).redux-saga
would certainly make our current thunk-based action-creators more declarative, but I admit I really don’t care for the generator syntax. I am interested in what FRP alternatives are out there. This comment in the same thread hints at this: Recommendations for best practices regarding action-creators, reducers, and selectors reduxjs/redux#1171 (comment). Not a lot of options out there from what I can tell. redux-cycles is no longer maintained, but that’s totally the right idea.refract
looks pretty good and is still maintained, though not actively.@shresthabijay @anmolarora1 @aqkhan Would love to hear any thoughts you have on any of this.
The text was updated successfully, but these errors were encountered: