4

Here is my reducer

const initialState = {
  pros: [''],
}

export const rootReducer = (state = initialState, action) => {
  switch (action.type) {
    case ACTION_ADD_PROS:
      return {
          ...state.pros,
          pros: [...state.pros,  action.payload]
        }

    case ACTION_CHANGE_PROS:
      return update(state, { 
        pros: { 
          [action.index]: {
            $set: action.payload
          }}
      });

    case ACTION_REMOVE_PROS:   

   ???


      return x
    default:
  }
  return state;
};

Can some one help me how can I remove current item from array? I have tried many ways but I don't understand what the problem here, also I have used react-addons-update for update store

2
  • you are removing an item based on id? or just the last or first one? Commented Sep 28, 2018 at 14:54
  • I'm trying to remove based on index Commented Sep 28, 2018 at 15:05

3 Answers 3

4

I think that Array.prototype.filter would be the most easy and readable solution to remove an item from an array:

return {
  pros: state.filter((item, index) => index !== action.index)
}
Sign up to request clarification or add additional context in comments.

Comments

1

i think this should do the trick ( haven't tested )

you need to create a copy of the array, remove the item, and then assign the new array to the state

const initialState = {
pros: [''],
}

export const rootReducer = (state = initialState, action) => {
switch (action.type) {
    case ACTION_ADD_PROS:
    return {
        ...state.pros,
        pros: [...state.pros,  action.payload]
        }

    case ACTION_CHANGE_PROS:
    return update(state, { 
        pros: { 
        [action.index]: {
            $set: action.payload
        }}
    });

    case ACTION_REMOVE_PROS:
    // make a copy of the array
    const newArr = state.pros
    // remove item at index 4
    newArr.splice(newArr.indexOf(action.payload), 1)   

    return update(state, {
           pros: [...newArr]
    })

   //this is the old code that is incorrect
   /* return update(state, { 
        pros: { 
        [action.index]: {
            $set: newArr
        }}
    });/*


    return x
    default:
}
return state;
};

for more information about the array.splice()

Remember: NEVER EDIT THE STATE DIRECTLY. the state needs to be immutable

edit: the return method was wrong, please try this one edit: as from your comment, i corrected the newArr also now it should remove the "current" value from the array basically you wan't to edit the copy of the array and then just assign the smaller array

2 Comments

It's not helped, maybe with ACTION_ADD_PROS something wrong?
also the action.payload is the current value of INPUT and not array
0

Instead of spliceing, just slice the array before the index, after the index, and return the combined array.

It'd keep the original array untouched

  [
    ...state.pros.slice(0, action.index),
    ...state.pros.slice(action.index + 1)
  ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.