Search code examples
javascriptreactjsreact-hooksarrow-functions

How to pass params with arrow function in ReactJS hooks


I am new to ReactJS, I am confused how to pass params with arrow functions, I tried to use .bind() to bind the value so i can use it in the function but it didn't work, and passing it like a normal function didn't work as well.

My Code

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>

Solution

  • 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>