My state values are as follows:
constructor(props) {
super(props);
this.state = {
list: [
{ id: '1', age: 42 },
{ id: '2', age: 33 },
{ id: '3', age: 68 },
],
};
}
And my handle function is
handle() {
this.setState(prevState => ({
list: {
...prevState.list,
[prevState.list[1]]: {
...prevState.list[1],
age: 40
}
}
}))
}
What I want here is to change the age
of second row from 33
to 40
, but my code is wrong. Can anyone suggest a correct way?
Required answer:
list: [
{ id: '1', age: 42 },
{ id: '2', age: 40 },
{ id: '3', age: 68 },
],