last day (15 days later) » 

19:41
1
Q: Sharing Redux actions between multiple reducers

sir_thursdayThis is a highly simplified description of the problem I'm running into with my React/Redux application: Problem description: Suppose I have a web app with two views, a ProductList view and a ProductOfTheDay view. Each view has its own "store" file; holding state, reducer, actions, etc assoc...

why would the reducers need to know which view the action came from? the result should be the same right? add the product to the cart? do you have a cart reducer?
Given my current state structure, they need to know how to find the corresponding Product in my state. The index parameter I am passing to addToCart allows each reducer to find the corresponding Product. No, I do not have a Cart reducer... let me think about how that would work.
what happens after you add something to the cart? wouldn't you need to store the current cart state somewhere?
Suppose in my case that we don't have any cart state. Maybe instead of addToCart, you can think of the button click as a markProductAsFavorite or something?
I like the idea of having a Cart store though, let me post it as a third solution.
well.. even "mark product as favorite" to ME means having a favorites array or set which is a list of product IDs. modifying the actual product seems like a bad solution to me.. and in your particular case its extra bad since you don't appear to have a "products" reducer, but reducers based on views, which is also a pretty unusual thing to do
19:41
Hey, thanks for helping. Why are reducers based on views bad/unusual?
Each view has its own state that, for the most part, is distinct from other views.
well because it doesn't represent the actual data in your app it seems
from what it sounds like
is there would be a state called products that is independent of views
and maybe you would have a single item of state called productOfTheDay which is jut an id.. that id is used to find the product in the products list
I see...
that seems simpler to me at least
and that answers your SO question as well
Yeah, unfort it's easy for me to fall into the habit of structuring my state "visually", i.e., how it is rendered on my app
But your suggestions make sense
where you simply have a single action called addToCart, and whatever reducer cares to listen to that action can handle it how it eems fit
19:44
Exactly
cool
Thanks!
np good luck

last day (15 days later) »