David Díaz
David Díaz

Reputation: 119

How to get value from function

I'm trying to call "handleClick" sending the index of the array like this:

{citas.map((cita, i) => (
    <tr className='clickable-row' onClick={handleClick.bind(i)} >
         <td>{cita.CheckIn}</td>
         <td>{cita.CheckOut}</td>
         <td>{cita.Precio}</td>
         <td>{cita.Servicio}</td>
    </tr>
 ))}

and this is the handleClick method:

const handleClick = (i) => {
    console.log(i)
    history.push("/citasDetalladas/" + ids[i]);
}

the problem is that "i" is not what is supposed to be (the array index), as it prints this: enter image description here

Upvotes: 1

Views: 61

Answers (1)

Quentin
Quentin

Reputation: 943562

The first argument to the bind method is the this value. The second is the first argument passed to the function:

onClick={handleClick.bind(null, i)}

You could also create your new function using an arrow function instead:

onClick={() => handleClick(i)}

Upvotes: 2

Related Questions