Updating the JSON state using setState is not happening
1.I want to change the particular quantity value in the JSON, for this I wrote an IncrementHandler method to a tag and implemented the function but it is not updating my main state
2.I implemented the setState method inside the function for updating the state.
import React, { Component } from 'react';
export default class App extends Component {
state = {
value: 0,
students: [
{
item: 'salad',
qty: 2,
department: 'Information Technology',
rollno: '123'
},
{
item: 'cheese',
qty: 2,
department: 'Computer Engineering',
rollno: '456'
},
{
item: 'meat',
qty: 2,
department: 'Information Technology',
rollno: '789'
}
]
};
render() {
const IncrementHandler = (type) => {
if (type === 'salad') {
this.setState((prevState) => ({
qty: this.state.students[0].qty + 1
}));
}
};
return (
<div>
<div>
<div>
<table border="2">
<tbody>
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Button</th>
</tr>
{this.state.students.map((item, i) => (
<tr key={i}>
<td>{item.item}</td>
<td>{item.qty}</td>
<td onClick={() => IncrementHandler(item.item)}>click</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
);
}
}