As per Reactjs.org to handle event and prevent default use below code:
function ActionLink() {
function handleClick(e) {
e.preventDefault();
console.log('The link was clicked.');
}
return (
<a href="#" onClick={handleClick}>
Click me
</a>
);
}
However, this also requires to add binding in constructor like:
this.handleClick = this.handleClick.bind(this);
I was able to get desired behaviour by below code:
<span>
<a href="#" onClick={()=>doSomething(arg1,agr2)}>Click here</a>
</span>
Question: Which one is better option? It seems first one requires to use stateful component and second option can do the things irrespective of component being stateful or stateless.
onClick={this.handleClick.bind(this)}in the first, where a new function is created on each render.