A utility to wrap many copies of a single Redux reducer into a single key-based reducer.
npm install multireducer-adailey14#multireducer-adailey14



multireducer is a utility to wrap many copies of a single Redux reducer into a single key-based reducer.
```
npm install --save multireducer-adailey14
I created this version of multireducer to solve a few issues with the original, and to provide semantics closer to connect() and bindActionCreators() provided by react-redux and react respectively. The three issues this solves are:
1. You can mount multireducer anywhere in your state tree, and use it more than once.
2. You can use this with react-thunk middleware
3. You don't have to wrap connectMultireducer components to pass in a multireducerKey, you can do it explicitly in the connectMultireducer call.
There are times when writing a Redux application where you might find yourself needing multiple copies of the same reducer. For example, you might need more than one list of the same type of object to be displayed. Rather than make a big reducer to handle list A, B, and C, and have action creators either in the form addToB(item) or addToList('B', item), it would be easier to write one "list" reducer, which is easier to write, reason about, and test, with a simpler add(item) API.
However, Redux won't let you do this:
`javascript
import list from './reducers/list';
const reducer = combineReducers({
a: list, // WRONG
b: list, // WRONG
c: list // WRONG
});
`
Each of those reducers is going to respond the same to every action.
This is where multireducer comes in. Multireducer lets you mount the same reducer any number of times in your Redux state tree, as long as you pass the key that you mounted it on to your connected component.
STEP 1: First you will need to wrap the reducer you want to copy.
`javascript
import multireducer from 'multireducer';
import list from './reducers/list';
const reducer = combineReducers({
myLists: multireducer({ // may be mounted anywhere
proposed : list,
scheduled : list,
active : list,
complete : list
})
});
`
STEP 2: Now use connectMultireducer() instead of react-redux's connect() to connect your component to the Redux store.
`javascript
import React, {Component, PropTypes} from 'react';
import {connectMultireducer, multireducerBindActionCreators} from 'multireducer';
import {add, remove} from './actions/list';
class ListComponent extends Component {
static propTypes = {
list: PropTypes.array.isRequired
}
render() {
const {add, list, remove} = this.props;
return (
// connectMultireducer has the same semantics as redux connect, except each function receives the multiReducer key as a second argument if it is passed in to the component as a prop
ListComponent = connectMultireducer(
(state, key) => ({ list: state.lists[key] }),
(dispatch, key) => multireducerBindActionCreators({add, remove}, key, dispatch)
)(ListComponent);
export default ListComponent;
`
STEP 3: Pass the appropriate multireducerKey prop to your decorated component.
`javascript`
render() {
return (
Lists
);
}
STEP 3 Alternative: Pass the appropriate multireducerKey in connectMultireducer explicitly, instead of passing it as a prop.
`javascript`
ListComponent = connectMultireducer(
(state) => ({ list: state.lists['proposed'] }),
(dispatch) => multireducerBindActionCreators({add, remove}, 'proposed', dispatch)
)(ListComponent);
will now catch these 'thunks' and add the multireducerKey to any actions they dispatch, so you don't have to do anything special.
API
$3
Wraps many reducers into one, much like Redux's
combineReducers() does, except that the reducer that multireducer creates will filter your actions by a multireducerKey, so that the right reducer gets the action. The key is appended to each action.type, so your actions must have a 'type' property.$3
react-redux's connect(), the only difference is connectMultireducer will pass the multireducerKey prop to the mapStateToProps and mapDispatchToProps functions as a second argument.##### -
mapStateToProps : Function> Similar to the
mapStateToProps passed to react-redux's connect(). The mapStateToProps function will receive the multireducerKey prop as a second argument if it was passed in to the component. You can also ignore this second argument, and pass in a hard-coded key here. This function is provided the global state exactly the same way as connect().##### -
mapDispatchToProps : Function> Similar to the
mapDispatchToProps passed to react-redux's connect(). The mapDispatchToProps function will receive the multireducerKey prop as a second argument if it was passed in to the component. You can also ignore this second argument, and pass in a hard-coded key here. This function is provided the global state exactly the same way as connect().$3
react's bindActionCreators(), the only difference is multireducerBindActionCreators takes the multireducerKey as an argument, and adds the multireducerKey to actions generated by the returned actionCreators.$3
Used to add the multireducerKey directly to an action object. Should only be necessary if you're doing something advanced.
$3
#### -
multireducerKey : String [required]> The key to the reducer in the
reducers object given to multireducer(). This will limit its state and actions to the corresponding reducer.
Working Example (Using the original multireducer!)
The react-redux-universal-hot-example project uses
multireducer. See its reducer.js, which combines the plain vanilla counter.js duck, to a multireducer. The CounterButton.js connects to the multireducer, and the Home.js calls with a multireducerKey` prop.