immutable-reducers
v1.1.0
Published
Create reducers for immutable data structures. Useful for redux.
Downloads
4
Readme
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 artistFansReducer = createReducer(['artis', 'fans'], (state, action) => {
switch (action.type) {
case 'NEW_FAN':
return state + action.count;
}
return state;
});
// Combine 'em up
const reducer = combineReducers(artistNameReducer, artistFansReducer);
// Step the state
reducer(initialState, { type: 'NEW_FAN', count: 1 });
createReducer
createReducer
helps you make a reducer that operates on a small area of an immutable data structure.
createReducer(
keyPath: Array<any>,
updater: (targetState: any, action: Object) => any,
state: any
): any
For example, given some initial state:
const initialState = fromJS({
user: {
favorites: Immutable.OrderedSet()
}
});
We can create a reducer that looks out for favorite actions and remembers the id:
const favoriteReducer = createReducer(['user', 'favorites'], (favorites, action) => {
switch (action.type) {
case 'FAVORITE':
return favorites.add(action.id);
}
return favorites;
});
createReducer
handles reaching into the data structure and updating the value.
Good to know:
- If the updater function returns the same value it was called with, then no change will occur.
- If the
keyPath
you specify does not exist, an ImmutableMap
will be created at each intermediary key. - The keys can be immutable data structures too #winning
- The third argument to the reducer is the whole state object, allowing you consult other areas of state when handling an action
combineReducers
combineReducers
is a less opinionated version of redux's default combineReducers
utility.
type Reducer = (state: any, action: Object) => any;
type ReducerObject = Object<string, Reducer>;
combineReducers(...Reducer|ReducerObject): Reducer
It has two useful forms: applied to a list of reducers or to an object. When applied to the list, it acts like redux's combineReducers.
When applied to an object, it's slightly different. immutable-reducers
will use the key of the object to 'scope' the reducer further.
For example, in the following example:
You could scope it to the user
key of your (immutable) state using combineReducers
:
const combineReducer = combineReducers({
user: createReducer(['favorites'], (favorites, action) => {
switch (action.type) {
case 'FAVORITE':
return favorites.add(action.id);
}
return favorites;
});
});
The end result is the same as the createReducers
example, where the favoritesReducer
will actually operate on the ['user', 'favorites']
key path.
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.