javascript - Reactjs: Is it necessary to copy object in component state before modifying it? -
suppose my reactjs component has 2 states:
a: { a: 1 }, b: [1, 2, 3]
now want them become:
a: { a: 1, b: true }, b: [1, 2, 3, 4]
is correct by:
this.state.a.b = true; b = this.state.b.push(4); this.setstate({ a: this.state.a, b: b });
if not, appropriate way it.
best way it.
this.setstate({ a: object.assign({}, this.state.a, { b: true }), b: [...this.state.b, 4] });
Comments
Post a Comment