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

前端 未结 7 1433
无人及你
无人及你 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:01

    Add e.preventDefault() to onClick and add href="#" attribute

    <a href="#" onClick={(e) => { e.preventDefault(); fields.push(); }}>Add Email</a>
    
    0 讨论(0)
  • 2021-01-01 16:01

    fields.push()}>Add Email

    In TypeScript href="javascript:void(0);" throws warning, so there any other way to skip that warnings

    0 讨论(0)
  • 2021-01-01 16:03

    try the following code snippet

    <a href="true" ....>
    
    0 讨论(0)
  • 2021-01-01 16:10

    You should consider write method clickHappens in react component instead of writing this inline. Hope it works!

    <a href onClick={(e) => {e.preventDefault(); fields.push()}}> Add Email </a>
    
    0 讨论(0)
  • 2021-01-01 16:14

    add javscript:void(0):

    <a href="javascript:void(0);" onClick={() => fields.push()}>Add Email</a>
    
    0 讨论(0)
  • 2021-01-01 16:16

    You should call preventDefault function from onClick event like this:

    class App extends React.Component {
      onClick = (e) => {
        e.preventDefault()
        console.log('onclick..')
      }
      render() {
        return (
          <a href onClick={this.onClick} >Click me</a>
        )
      }
    }
    

    You can use something like this particular to your use case:

        const renderEmails = ({ fields }) => (
          ...
          <a href onClick={(e) => {
            e.preventDefault()
            fields.push()
          }}>Add Email</a>
          ...
        )
    
    0 讨论(0)
提交回复
热议问题