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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章