Redux removing item from array

Tell us what’s happening:

Hi, I’m new to Redux (recently learned the basic of React) and learning it with freecodecamp tutorials.

In the tutorials, I couldn’t understand what the model answer code is doing and tutorial doesn’t explain exactly what it is doing.

Could someone kindly help me to understand what does this code doing?

I understand that immutableReducer function return new state array,
but I don’t fully get where action.index come from (since removeItem just returns Object containing type and index) and what state.length means. (Is that the string length of ‘REMOVE_ITEM’? But why its length is necessary here? )

Thank you in advance.

Your code so far


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);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0.

Challenge: Remove an Item from an Array

Link to the challenge:
https://www.freecodecamp.org/learn/front-end-libraries/redux/remove-an-item-from-an-array

state is an array. state.length is the length of the array.

How did this code work?

did u find the answer ?

1 Like

Hi @ajiq !

Welcome to the forum!

The original poster for this question has not been active on the forum for over a year.

If you have a question about a challenge I would suggest opening up a new post and asking there.

Thanks!

1 Like