Updating the array object in React state using immutability helper
Im importing update
from immutability helper here :)
this.state = {
a: {
b: [{ c: '', d: ''}, ...]
}
}
this.setState((prevState, props) => update(prevState, {
a: {
b: {
$apply: b => b.map((item, ii) => {
if(ii !== n) return item;
return {
...item,
c: 'new value'
}
})
}
}
}))
One way to do it would be using $set
let index = 0;
let newState = update(this.state, {
a: {
b: {
[index]: {
c: { $set: "new value"}
}
}
}
});
this.setState(newState);
jsfiddle