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

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -