search

Home  >  Q&A  >  body text

javascript - Can the raact setState() method only modify some properties of an object?

this.state.style:{
width:
height:
color
}
//修改属性
this.setState({style:{}})

Now I want to add an attribute to it through setState. Can I only add one attribute and keep the other attributes with their original values?

给我你的怀抱给我你的怀抱2793 days ago504

reply all(1)I'll reply

  • 漂亮男人

    漂亮男人2017-05-19 10:35:56

    When using Object.assign(), be careful not to return the original object, but a new object:

    this.setState(Object.assign({}, this.state.style, {width: 666}))

    reply
    0
  • Cancelreply