erikras / react-redux-universal-hot-example

A starter boilerplate for a universal webapp using express, react, redux, webpack, and react-transform
MIT License
12.01k stars 2.5k forks source link

Multiple reducers using the same action type constant #1311

Closed ttaghiyev closed 7 years ago

ttaghiyev commented 7 years ago

Maybe I missed this, but given the way this set up defines constants:

const INCREMENT = 'redux-example/counter/INCREMENT';

what happens, or how would you handle, a case where multiple reducers look for the same action type. A simple example is a todo app where there there is a stats component that displays a variety of statistics. Adding a todo (hypothetically a ADD_TODO) would want to trigger both a literal addition, and a recalculation of the stats, so it seems to me that both the todos and the stats reducer would listen for ADD_TODO. Would I define the constant in the file its closest tied to and export it for others? The goal here looks to be to compartmentalize var exposure so is it ok for one reducer to provide constants to others?

Doubl3 commented 7 years ago

You could just export this const and then import it right into the other reducer(s). Use it into your switch statements as done with the other ones. Be careful to not forget to remove its imported ref when deleted from its source reducer.

ttaghiyev commented 7 years ago

@Doubl3 my biggest concern with that was that if the reducers aren't all in the same place, you'd have a ton of import statements cross referencing files all over the place.

Since the question though I've come up with a new strategy that's been working well. I export a reducer and all of it's associated files (constants, action types, selectors) as a single default which lets me import it as a 'bundle' into another file and then destruct the constants from there. If a bundle is only used by one Component, i keep it there directory wise. If its used by more than one, it goes to a top level directory where components have quick access to it.

Doubl3 commented 7 years ago

I didn't exactly catch your pain. Thx for your explanations. Indeed, it's a good way to go.