Tell us what’s happening:
SPOILER: The answer to the exercise is written below.
Why do I need the spread operator there, if I’m calling slice? I don’t quite understand.
Your code so far
const immutableReducer = (state = [0,1,2,3,4,5], action) => {
switch(action.type) {
case 'REMOVE_ITEM':
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);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36
.
Challenge: Remove an Item from an Array
Link to the challenge: