0

So I have a state array containing objects, I want to update a specific value of a given object, given the object index.

const initialState = {
cars:[ {name:  "golf", speed: 14, price:444}, { name:"benz", speed: 15, price:6666}]

}

so this is a sample state, what I want to do is, given the index of the object(e.g the second object) I want to update the speed to 15

my Action is

const setSpeed = (index) => {
    console.log(index);
    dispatch({
      type: SET_SPEED,
      payload: {
        respond: index,
      },
    });
  };

Above is my action that dispatches a type of SET_SPEED, and the index of the object as a payload.

I'm trying to write a reducer that update the speed by one given the index of the object

labs
  • 790
  • 6
  • 8
  • Does this answer your question? [Update one of the objects in array, in an immutable way](https://stackoverflow.com/questions/43792457/update-one-of-the-objects-in-array-in-an-immutable-way) – diedu Oct 23 '21 at 20:36
  • I want to update the value with a reducer function – labs Oct 23 '21 at 20:45
  • https://stackoverflow.com/questions/35628774/how-to-update-single-value-inside-specific-array-item-in-redux – diedu Oct 23 '21 at 20:52

0 Answers0