what is the preferred way to mutate a React state?

前端 未结 4 1131

Let\'s say I have a list of plain objects in my this.state.list that I can then use to render a list of children. What then is the right way to insert object in

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-07 10:03

    setState() can be called with a function as a parameter:

    this.setState((state) => ({ list: state.list.concat(newObj) }))
    

    or in ES5:

    this.setState(function(state) {
      return {
       list: state.list.concat(newObj)
      }
    })
    

提交回复
热议问题