onClick
expects a function. An arrow function does not have its own this
; the this
value of the enclosing execution context is used.
Arrow function is a replacement for the following
onClick={this.handleClick.bind(this,i)}
It doesn’t work when you run it like
onClick={this.handleClick(i)}
because in this case it will call a function and that will pass a return value that will be evaluate everytime render is called. So if you are doing somethings in the onClick function that causes a rerender for instance setState
you app will go in an endless loop. Thus onClick
needs a function and not a value so unless you are returning a function from the onClick
handler you should not directly call it.
Arrow function above performs the role of binding the parameter to the function