React why onClick property function triggers without click)

Well you don’t have to use .bind().

All that does is create a new function with the this property set to whatever your first arguments is in the .bind() call.

The real issue was that your code which didn’t work wasn’t passing a function at all. It was passing the result of a call to your handler function. That’s why it looked like you were getting an event triggered - instead of passing a function, you were calling the handler function while creating the component.

In the code you posted which did work, it worked because you created a function (using arrow function syntax) which was passed in as the event handler function. When the event is triggered, that arrow function is called and inside of the arrow function the handler function you want is called.

This is largely the same as using .bind(). Unless your handler function needs ‘this’ as a specific object, there is no need to use .bind().

2 Likes