Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

15
  • 7
    ^^ just .bind(null, attributeId) Commented Nov 28, 2015 at 1:16
  • 2
    That would also run bind on every re-render... which is sub-optimal, to say the least. Commented Jul 26, 2016 at 8:12
  • 5
    @George Still would run bind on every render call. Wasteful. The most optimal way, IMO, is to do it in constructor: this.onclick = this.handleClick.bind(this); Then, in handleClick, retrieve attributeId from the React event: const {attributeId} = event.target. Commented Oct 5, 2016 at 15:11
  • 4
    Here is a CodePen illustrating the approach. Commented Oct 5, 2016 at 15:18
  • 3
    Zen is correct - the pollution of the render method is not necessary if you use a constructor within the component. While this answer works, and might even be in some doc examples, the constructor will suit you better if you would like to maximize efficiency. The largest problem with most react apps is the render method containing too much weight, or in other cases firing unchecked. Commented Oct 20, 2016 at 19:24