Skip to content Skip to sidebar Skip to footer

Passing Id With Onclick Function React

I am having problem with passing id to handleClick function and console.log'ing it into the console. I have tried simple example which works but i can not understand why it doesn't

Solution 1:

You can try the following:

<div id={index} onClick={(e) =>this.handleClick(index,e)} /> 

And override the function like this:

handleClick(id, event){console.log(id)}

You can find the fiddle here

Solution 2:

Looks like there's a whoooole lot of unnecessary code in your question, try to make a nice concise example. I think what you mean is how to pass the id as an argument to the handle click function? It could be done using an arrow function.

<divid={index}onClick={() => this.handleClick(index)} />

Post a Comment for "Passing Id With Onclick Function React"