You have only two errors with {} instead of [] and forget to use Object.assign .
const reducer = (state = {}, action) => { switch (action.type) { case 'addConnection': return Object.assign({}, state, { connections: [ ...state.connections, { [actions.compositeKey]: action.connection } ] }); default: return state; } } export default reducer;
This can help to see how this is expressed. He does the same, but I think he reads a little nicer
const reducer = (state = {}, {type, compositeKey, connection}) => { switch (type) { case 'addConnection': return Object.assign({}, state, { connections: state.connections.concat({ [compositeKey]: connection }) }); default: return state; } } export default reducer;
Or if you are using Immutable , something like this
import Immutable from 'immutable'; const reducer = (state = Immutable.Map(), {type, compositeKey, connection}) => { switch (type) { case 'addConnection': return state.set( 'connections', state.get('connections').concat({ [compositeKey]: connection }) ); default: return state; } } export default reducer;
source share