I have the following html element:
<a href onClick={() => fields.push()}>Add Email</a>
I need the href attribute so bootstrap styles the element with a link styling (color, cursor).
Problem is, if I click that now it causes the browser to redirect. How can I update the above to not redirect the browser onClick but still run fields.push()
?
try the following code snippet
fields.push()}>Add Email
In TypeScript
href="javascript:void(0);"
throws warning, so there any other way to skip that warningsYou should consider write method clickHappens in react component instead of writing this inline. Hope it works!
Here's a simple solution,
Basically what react wants us to do is to use a different component, for instance a button. We can always make a button look like a link using CSS(and thus, won't contain
href
and the complexity to remove it's behaviour). So, instead of using an anchor tag as a button, use the button element itself. Hope it makes sense.add javscript:void(0):
You should call preventDefault function from onClick event like this:
You can use something like this particular to your use case: