erikras / multireducer

A utility to wrap many copies of a single Redux reducer into a single key-based reducer.
MIT License
422 stars 23 forks source link

dynamically create new copies #102

Open brennanerbz opened 8 years ago

brennanerbz commented 8 years ago

Is there a way to dynamically add/create copies?

Using your universal example, if the user took an action to create a new counter-- how would a new copy of the counter reducer be created?

adailey14 commented 8 years ago

@brennanerbz I'm pretty sure multireducer does not support dynamic copies. I think you are better off writing your own reducer in that case and passing a key around that you can pass into your actions and use to access/manipulate many copies of your store data.

Look at how redux-form works for example - there is a single form reducer that creates many copies/instances of forms. I've used this pattern a lot and it works well. I've found that once you're comfortable writing reducers in that pattern there is little need for multireducer and it's almost always better to write one of these 'instance managing reducers' in case you need to support dynamic copies.

erikras commented 8 years ago

Yeah, not really supported. Interesting idea, though.

jephjohnson commented 7 years ago

Any movement on this? Doesn't seem to work, if you're using your mulitreducer in an array but the length is constantly changing. I am fetching an api, so sometimes the results is 5, 10, 12, 15 and so on...Any thoughts on how to solve that? Not unless I say something along the lines of:

const thereducer = combineReducers({
  toggleCollection: multireducer({
    toggle0 : toggle,
    toggle1 : toggle,
    toggle2 : toggle,
    toggle3 : toggle,
    toggle4 : toggle,
    toggle5 : toggle,
    toggle6 : toggle,
    toggle7 : toggle,
    toggle8 : toggle,
    toggle9 : toggle,
    toggle10 : toggle
  }),
einarq commented 6 years ago

@adailey14 Do you have an example like that you could point me to? I have the same need myself quite often, but haven't found a good example yet. Usually end up having a byId map in the reducer, and every action has the key and needs to do a deep update in the map. Not the most elegant solution...