add row without push in es6 for react state

前端 未结 2 1377
别跟我提以往
别跟我提以往 2021-01-25 08:32

I\'m not sure I\'m doing the right thing, I mutate variable outside of setState, it\'s fine right? or there\'s more elegant way to do it?

state = {
    persons:          


        
2条回答
  •  余生分开走
    2021-01-25 09:19

    In my opinion, more elegant way would be to use functional setState:

    const newPerson = { name: '', age: -1 };
    this.setState(prevState => ({ persons: [...prevState.persons, newPerson] })
    

提交回复
热议问题