Last active
June 5, 2018 11:01
-
-
Save kettanaito/2dd13fca87968273b54613fd4b2281d7 to your computer and use it in GitHub Desktop.
Redux - Immutability
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Record, List } from 'immutable' | |
const CartInitialState = new Record({ | |
items: List() | |
}) | |
const initialState = CartInitialState({ | |
items: List() | |
}) | |
export default initialState |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import initialState from './initial-state' | |
export default function cart(state = initialState, action) { | |
switch (action.type) { | |
case 'ADD_ITEM': { | |
const { item } = action | |
const nextItems = state.update('items', items => items.concat(item)) | |
return state.set('items', nextItems) | |
} | |
case 'REMOVE_ITEM': { | |
const { itemId } = action | |
const nextItems = state.get('items').filterNot(item => item.id === itemId) | |
return state.set('items', nextItems) | |
} | |
default: | |
return state | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment