Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

mcfly-combinereducers

1.0.3 • Public • Published

McFly CombineReducers

Util to use in a McFly-Store's callback.
It turns an object whose values are different reducer functions, into a single reducerFunction.

usage

var Redux  = require('redux');
, reducerCollection  = require('../actions/reducers')
, McFly  = require('mcfly') // '../flux/mcFly'
, combineReducers  = require('mcfly-combinereducers')
, combinedReducers  = combineReducers(reducerCollection)
, _state  = _getState() // however you'll get it
, MsgStore  = McFly.createStore({
        getState: function() {
            return this.state;
        }
    },  function(action) {
            // switch(action.type) { ... }
            // outsource your reducers and use:
            combinedReducers(_state, action);
            // standard McFly procedure
            MsgStore.emit('change');
            return true;
        }
    )
;
module.exports  = MsgStore;
// actions/reducers.js
var ReducerCollection  = {
        'todo':  function(state, action) {
            switch (action.type) {
                case 'SEND_MSG':
                    _state.todos.push(action.payload);
                default:
                    return _state;
           }
        }
    , 'counter':  function(state, action) {
            switch (action.type) {
                case 'INCREMENT':
                    return _state.counter + 1;
                case 'DECREMENT':
                    return _state.counter - 1;
                default:
                    return _state;
            }
        }
    }
;
module.exports  = ReducerCollection;

API

@param {object} ReducerCollection - your reducer functions
@return {func} combinedReducers - your new reducer function
combineReducers(ReducerCollection)
@param {object} state - your App/Component State
@param {object} action - a flux standard action object
@return {bool} success - true at success
combinedReducers(state, action);

Install

npm i mcfly-combinereducers

DownloadsWeekly Downloads

3

Version

1.0.3

License

MIT

Last publish

Collaborators

  • avatar