React's array traversal rendering

React's array traversal rendering and push into the array case

    constructor(props) {
        super(props)
        this.state={
            arr: [1, 2, 3, 4]
        }
    }
   addItim =()=>{
       this.setState(()=>{//将数字push到原来的数组中
           this.state.arr.push(66)
           //直接返回出来
           return{
               arr:this.state.arr
           }
       })
   }
    render() {
        const { arr } = this.state//解构赋值
        return (
            <div>
                <button onClick={this.addItim}>加数字</button>
                <ul>
                    {/* {arr.map(item => <li>{item}</li>)} */}
                    {/* 循环遍历上面的数组 放在li里面 */}
                    {arr.map((item,index) => <li key={index}>{item}</li>)}


                </ul>
            </div>
        )
    }
}
export default traversal

If the above key is not added, the following console will report an errorInsert picture description here

Guess you like

Origin blog.csdn.net/weixin_45663264/article/details/102615730