2 回答
TA贡献1829条经验 获得超6个赞
there are two ways to do it
1. copy the object into some:
handler = () => {
let some = Object.assign({}, this.state.some);
some.z.a = 1111;
this.setState({ some }, () => {
console.log(this.state);
});
};
will give an output:
{
name: "React"
some: Object
name: "axys"
a: Array[5]
z: Object
a: 1111
b: 5
c: 6}
2. using the spead oprator
handler = () => {
let some = { ...this.state.some };
console.log(some)
this.setState(
prevstate => ({
some: {
z:{ ...prevstate.some.z,
a: 1111}
}
}),
() => {
console.log(this.state);
}
);
};
with solution:
{name: "React"
some: Object
z: Object
a: 1111
b: 5
c: 6}
hope it will help
添加回答
举报