In React, how can I cause anchors to do nothing on click?

前端 未结 7 1434
无人及你
无人及你 2021-01-01 15:51

I have the following html element:

 fields.push()}>Add Email

I need the href attribute so bootstra

相关标签:
7条回答
  • 2021-01-01 16:22

    Here's a simple solution,

    • On React class component
    class App extends React.Component {
      onClick() {
        console.log('onclick..')
      }
    
      render() {
        return (
          <a href={void(0)} onClick={this.onClick} >Click me</a>
        )
      }
    }
    

    React is dropping the javascript:void(0) solution and the href doesn't certainly accept the empty attribute value.

    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.

    0 讨论(0)
提交回复
热议问题