Skip to content

Instantly share code, notes, and snippets.

@toruticas
Last active May 27, 2019 02:19
Show Gist options
  • Save toruticas/04d02de8acea42167175c420900de2ba to your computer and use it in GitHub Desktop.
Save toruticas/04d02de8acea42167175c420900de2ba to your computer and use it in GitHub Desktop.
Redux Architecture Example (reducer)
import { List, Map } from 'immutable';
export const scaffold = Map({
currentIndex: 1,
inputValue: '',
items: List(),
});
export default (state = scaffold, action) => {
switch (action.type) {
case 'CHANGE_INPUT_VALUE':
return state
.set('inputValue', action.value);
case 'ADD_ITEM':
return state
.update('items', item => item.push(action.item))
.update('currentIndex', value => value + 1);
case 'REMOVE_ITEM':
return state
.update('items',
value => value.filter(item => item.get('id') !== action.id)
);
default:
return state;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment