React的定义函数传值

1.通过bind进行传值

       this.函数名.bind(this,参数)

    

传递参数

 <button onClick={this.changeNum.bind(this, key, 1)}>+</button>

 定义函数

 changeNum(index, num) {
        // console.log(index,num)
        this.state.shopList[index].num += num;

        this.setState({
            shopList: this.state.shopList
        })

    }

2.通过es6的方式进行传值

传递参数

<button onClick={()=> this.bind(key) }>删除</button>

定义函数

    //删除数据
    delect = (index)=> {
        //删除数据
        this.state.shopList.splice(index,1)

        this.setState({
            shopList: this.state.shopList
        })

    }

 

发布了55 篇原创文章 · 获赞 12 · 访问量 4244
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章