0

I have a loop that displays data and when clicking the data it should run a method that gets the clicked item.

ie:

items.map(item => {
    return (
        <div
               onClick={someMethod(item)}>
               Some content
                </div>
     );
}

The example won’t work properly but it’s just to show the situation. I know 2 options but both will make the template render each time:

  1. Use arrow function

    onClick={()=>someMethod(item)}
    
  2. Use bind

    onClick={someMethod.bind(this, item)}
    

Any better ideas that won’t cause rerender?

Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
Tzook Bar Noy
  • 11,337
  • 14
  • 51
  • 82
  • I know 2 options but both will make the template render each time: ? does that re render on click ? – Vivek Doshi Oct 25 '17 at 06:46
  • yea explain how do you know it renders 2x – omarjmh Oct 25 '17 at 06:47
  • Didn’t check yet, just trusted another question I read hear from google. If one of those ways are good so that’s great – Tzook Bar Noy Oct 25 '17 at 06:49
  • This is the Q https://stackoverflow.com/questions/46642018/divs-id-attribute-is-undefined-not-getting-captured-onclick-in-a-react-app/46642251#46642251 – Tzook Bar Noy Oct 25 '17 at 06:53
  • It is difficult to provide an answer to your question until you describe precisely the behavior you see when you run the code yourself rather than the behavior you anticipate will happen from what you read elsewhere. – 223seneca Oct 25 '17 at 06:53

0 Answers0