如何在ReactJS钩子中使用箭头函数传递参数

问题描述 投票:0回答:1

[我是ReactJS的新手,我对如何使用箭头函数传递参数感到困惑,我试图使用.bind()绑定值,以便可以在function中使用它,但是它没有用,并且像普通函数一样传递它也不起作用。

我的代码

const getCheckById = (checkID) =>{
    console.log(checkID)
}

<div className=''>
    {
        // allChecks is an array of objects
        allChecks.map((el,index) => {
        return <div key ={index}>
                <span onClick={getCheckById(el.checkID)}>+</span>
            </div>
        })
    }
</div>
javascript reactjs react-hooks arrow-functions
1个回答
0
投票
const getCheckById = (checkID) =>{
    console.log(checkID)
}


<div className=''>
    {
        // allChecks is an array of objects
        allChecks.map((el,index) => {
        return <div key ={index}>
                <span onClick={() => getCheckById(el.checkID)}>+</span>
            </div>
        })
    }
</div>
© www.soinside.com 2019 - 2024. All rights reserved.