To answer your question, you have nothing to do to pass the arguments to your this._onClick
function.
The correct revised code would be:
class Foo extends React.Component { constructor() { super(); this._onClick = this._onClick.bind(this); } render() { return ( <div onClick={() => this._onClick(1, 2)}> Hello! </div> ); } _onClick = (a, b) => { console.log(a, b); } }
Secondly, the way you call this._onClick
is not the right way to call a function on click
.
Currently, what happens in every visualization process called by your function, because you did not pass the function as an argument, but you called this function and assigned its return value to support onClick.
you need to do it like:
render() { return ( <div onClick={() => this._onClick(prop1, prop2)}> Hello! </div> ); }
By calling your function in this way, you guarantee that this._onClick
will be called when the click event occurs.
source share