multireducer-adailey14

1.0.7 • Public • Published

#multireducer-adailey14

NPM Version NPM Downloads Build Status

multireducer is a utility to wrap many copies of a single Redux reducer into a single key-based reducer.

Installation

npm install --save multireducer-adailey14

Changes in this adailey version

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.

Why?

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:

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.

How It Works

STEP 1: First you will need to wrap the reducer you want to copy.

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.

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 (
      <div>
        <button onClick={() => add('New Item')}>Add</button>
        <ul>
          {list.map((item, index) =>
            <li key={index}>
              {item}
              (<button onClick={() => remove(item)}>X</button>)
            </li>)}
        </ul>
      </div>
    );
  }
}

// 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.

render() {
  return (
    <div>
      <h1>Lists</h1>
      <ListComponent multireducerKey="proposed"/>
      <ListComponent multireducerKey="scheduled"/>
      <ListComponent multireducerKey="active"/>
      <ListComponent multireducerKey="complete"/>
    </div>
  );
}

STEP 3 Alternative: Pass the appropriate multireducerKey in connectMultireducer explicitly, instead of passing it as a prop.

ListComponent = connectMultireducer(
  (state) => ({ list: state.lists['proposed'] }),
  (dispatch) => multireducerBindActionCreators({add, remove}, 'proposed', dispatch)
)(ListComponent);

Use with 'thunk' middleware

A common redux pattern is to use middleware that allows you to return a function from an action creator. multireducerBindActionCreators will now catch these 'thunks' and add the multireducerKey to any actions they dispatch, so you don't have to do anything special.

API

multireducer(reducers:Object) : Function

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.

connectMultireducer(mapStateToProps:Function?, mapDispatchToProps:Function?, ...rest) : Function

Creates a higher order component decorator, much like 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().

multireducerBindActionCreators(actionCreators:Object, multireducerKey:string, dispatch:Function) : Object

Acts much like 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.

multireducerWrapAction(action:Object, multireducerKey:string) : Object

Used to add the multireducerKey directly to an action object. Should only be necessary if you're doing something advanced.

Props to your decorated component

-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 <CounterButton/> with a multireducerKey prop.

Package Sidebar

Install

npm i multireducer-adailey14

Weekly Downloads

0

Version

1.0.7

License

MIT

Last publish

Collaborators

  • adailey14