2
nameChangedHandler = (id, event) => {
    let index = id;
    const updatedName = event.target.value;
    this.setState({
      persons: update(this.state.persons, { index: { name: { $set: updatedName } } }),
    });
  };

If I hardcode the index to any number the above code is working i.e ( update(this.state.persons, { 0: { name: { $set: updatedName } } }) )

Kindly Suggest a Solution.

Tholle
  • 108,070
  • 19
  • 198
  • 189

2 Answers2

2

replace { index: ... } with { [index]: ... }

Abdullah
  • 2,015
  • 2
  • 20
  • 29
1

You can use a computed property to use the value of the index variable:

this.setState({
  persons: update(this.state.persons, { [index]: { name: { $set: updatedName } } }),
});
Tholle
  • 108,070
  • 19
  • 198
  • 189