immutable-reducers
Create reducers for immutable data structures. Useful for redux.
Table of contents
Install
npm install --save immutable-reducers
Use
import { fromJS } from 'immutable';
import { combineReducers, createReducer } from 'immutable-reducers';
// Setup some state (probably your app state)
const initialState = fromJS({
artist: {
name: {
first: 'Sean',
last: 'Combs'
},
fans: 0
}
});
// Create some reducers
const artistNameReducer = createReducer(['artist', 'name'], (state, action) => {
switch (action.type) {
case 'RENAME':
return fromJS(action.value);
}
return state;
});
// You can scope them by combining with an object
const fansReducer = createReducer(['fans'], (state, action) => {
switch (action.type) {
case 'NEW_FAN':
return state + action.count;
}
return state;
});
const artistFansReducer = combineReducers({
artist: fansReducer // the full path is then ['artist', 'fans']
});
// Combine 'em up
const reducer = combineReducers(artistNameReducer, artistFansReducer);
// Step the state
reducer(initialState, { type: 'NEW_FAN', count: 1 });
Contributing
Please read the contribution guidelines. Contributions are welcome!
Thanks
Thanks to rackt for redux and all those who work on immutable.
License
Copyright (c) 2015 Tom Ashworth. Released under the MIT license.