简体   繁体   中英

how to setState object inside array in the state

i have an array of objects called messagesObj and i am trying to setState the content in messagesObj and nothing worked.

this.state = {
    messagesObj:[{
        name:"",
        time:"",
        content:""
    }]
}

You can set state of messageObj using spread operator like this

using spread operator

this.setState({
  messagesObj: [
    ...this.state.messagesObj,
    content: "your content"
  ]
})

Please let me know if you have any problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM