0.0.4 • Published 8 years ago
immutable-reducer v0.0.4
Dependencies
- ExtendableRecord
- (transitive) ImmutableJS
Getting Started
npm install immutable-reducer --save
Examples
Usage
appReducer.js
import { ImmutableReducer } from 'immutable-reducer';
import { createReducer } from 'redux-caller';
import { SET_USER, CLEAR_USER } from './actions.js';
import moment from 'moment';
class UserReducer extends ImmutableReducer {
[SET_USER]({payload}) {
return this.set('user', payload);
}
[CLEAR_USER]() {
return this.set('user', null);
}
getUserAge() {
const now = moment();
const birth = moment(this.user.dob);
return now.diff(birth, 'years');
}
}
AppReducer.defaultProperties = {
user: null
};
export const userReducer = createReducer(new UserReducer());
appStore.js
import { createStore, combineReducers } from 'redux'
import { userReducer } from 'redux-caller';
export const store = createStore(combineReducers({
user: userReducer
}));
actions.js
export const SET_USER = 'SET_USER';
export const CLEAR_USER = 'CLEAR_USER';
export const login = (user) => ({
type: SET_USER,
payload: user
});
export const logout = () => ({
type: CLEAR_USER
});
Credits
- ImmutableJS for underlying data structures
- npm-starter
- Airbnb for the work they've put into the javascript style guide and into the ESlint package.
License
MIT @ Joe Delgado