How can I insert into React's state array with setState?

后端 未结 5 2012
礼貌的吻别
礼貌的吻别 2021-02-01 18:42

I\'m looking to modify and array in react and insert elements on specific index. This is how my state looks like:

this.state = {arr: [\'\', \'\', \'\', \'\' ]}
<         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-01 19:19

    use spread operator https://codeburst.io/javascript-es6-the-spread-syntax-f5c35525f754

    let newChild = "newChild"
    
    this.setState({
        children: [
            ...this.state.children,
            newChild
        ]
    })
    

提交回复
热议问题