Skip to content

Instantly share code, notes, and snippets.

@jepras
Created October 17, 2018 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jepras/1613eb0897769be4f058be532691d689 to your computer and use it in GitHub Desktop.
Save jepras/1613eb0897769be4f058be532691d689 to your computer and use it in GitHub Desktop.
Use spread operator. Start to slice from beginning until index, then add a slice starting from index + 1 until end.
const immutableReducer = (state = [0,1,2,3,4,5], action) => {
switch(action.type) {
case 'REMOVE_ITEM':
// don't mutate state here or the tests will fail
return [
...state.slice(0, action.index),
...state.slice(action.index + 1, state.length)
]
default:
return state;
}
};
const removeItem = (index) => {
return {
type: 'REMOVE_ITEM',
index
}
}
const store = Redux.createStore(immutableReducer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment