0

Working with React/Redux to do some CRUD operations, and I need to update an object that is housed in an array in my state. How would I go about doing that?

My state looks like this:

var state = {
  insiders: [{object},{object}]
}

and My action object looks like this:

action = {
  type: 'UPDATE_INSIDER',
  payload: {
    insider: _insider
  }
}

I was originally thinking the reducer case would look like this, but I'm not sure. (only been working with react/redux since last week)

case  UPDATE_INSIDER:
  let insiders = state.insiders;
  insiders = insiders.filter(x => x.firstName !== payload.insider.firstName);
  return{ ...state, insiders: insiders.concat(payload.insider) }
  1. Does this work?
  2. Can it work better?
Chris Rutherford
  • 1,592
  • 3
  • 22
  • 58
  • yes it will work, but item will loose the index, updated item will always get the last index, you can use map also, that will not change the order of the elements. check duplicate answer, let me know if it fails to solve your issue. – Mayank Shukla Mar 20 '18 at 18:41
  • oh, it's not a problem yet, just asking before I got there. Thanks! – Chris Rutherford Mar 20 '18 at 18:44

0 Answers0