mayooran mayooran - 1 year ago 52
TypeScript Question

Button click event not getting fired when arguments are passed

I have this button click event at which I call a method which takes arguments. Below is how I assign the onClick event.

<button value="My Message" style={ buttonStyle } onClick= {() => this.myScanButtonClicked('Name') }>Send NameScan</button>

Below is the signature of the myScanButtonClicked which is inside the same class.

myScanButtonClicked = (item: string) => {

When remove the method argument it gets called. But with the argument, the method is not getting called on button click. But it gets executed at page load. I cannot figure out what's wrong here. Any help would be much appreciated.

I have tried the below shown way of calling it as well, but still no use.

onClick= {() => this.myScanButtonClicked.bind(this,'Mark') }

Answer Source

Try something like this :

onClick= {this.myScanButtonClicked.bind(this,'Mark')}

myScanButtonClicked(item, event){
   console.log(item); //Should be Mark