Easier way to to disable link in React?

前端 未结 7 435
逝去的感伤
逝去的感伤 2021-01-02 04:56

I want to disable Link in some condition:

render() {
    return (
  • {this.props.canClick ? Test
  • 7条回答
    •  佛祖请我去吃肉
      2021-01-02 05:54

      A good solution is using onClick() with event object. just do this in your jsx:

       this._onClick(e)}
      

      and in your _onClick function:

      _onClick = (e) => {
          e.preventDefault()
      }
      

      Complete Example in React:

      import React, { Component } from 'react'
      import {Link} from 'react-router-dom'
      
      export default class List extends Component {
          _onClick = (e) => {
              e.preventDefault()
          }
      
          render(){
              return(
                  
      this._onClick(e)}
      ) } }

    提交回复
    热议问题