If you have an array as part of your state, and that array contains objects, whats an easy way to update the state with a change to one of those objects?
Example, mo
Trying to clean up/ explain better how to do this AND what's going on.
update
the element at that indexsetState
with the new collectionimport update from 'immutability-helper';
// this.state = { employees: [{id: 1, name: 'Obama'}, {id: 2, name: 'Trump'}] }
updateEmployee(employee) {
const index = this.state.employees.findIndex((emp) => emp.id === employee.id);
const updatedEmployees = update(this.state.employees, {$splice: [[index, 1, employee]]}); // array.splice(start, deleteCount, item1)
this.setState({employees: updatedEmployees});
}
const index = this.state.employees.findIndex(emp => emp.id === employee.id),
employees = [...this.state.employees] // important to create a copy, otherwise you'll modify state outside of setState call
employees[index] = employee;
this.setState({employees});