1

I'm trying to make a change on a page, once addListItem is ran an array called "list" that is actually a redux state, needs to be updated. I managed to update it, but instead of an array I return an object. I need to return an array instead, but I don't know how to refactor the code to make it do that.

/**
 * Add Item
 */
case 'playlist/addListItem_success': {
  return {
    ...state,
    list: {
      ...state.list,
      [action.meta.position]: {
        ...state.list[action.meta.position],
        status: true
      }
    }
  }
}

1 Answer 1

2

To return an array, you'd have to use the array-spread syntax (e.g. [...someArray]) instead of object spread, but you can't use that to update a particular index. With a map you can elegantly express what you need though:

return {
  ...state,
  list: state.list.map((e, i) => i === action.meta.position ? {...e, status: true} : e)
};
Sign up to request clarification or add additional context in comments.

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.