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.

0

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

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 } } }),
});

2 Comments

Why only index we have to pass as computed property why not updatedName?
@AravindVijayan The right hand side of { key: value } will always be the value of that variable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.