react-router: How to disable a <Link>, if its active?

后端 未结 9 937
谎友^
谎友^ 2020-12-14 06:10

How can I disable a in react-router, if its URL already active? E.g. if my URL wouldn\'t change on a click on I want to p

相关标签:
9条回答
  • 2020-12-14 06:18

    This works for me:

    <Link to={isActive ? '/link-to-route' : '#'} />
    
    0 讨论(0)
  • 2020-12-14 06:19

    You can use CSS's pointer-events attribute. This will work with most of the browsers. For example your JS code:

    class Foo extends React.Component {
      render() {
        return (
          <Link to='/bar' className='disabled-link'>Bar</Link>
        );
      }
    }
    

    and CSS:

    .disabled-link {
      pointer-events: none;
    }
    

    Links:

    • pointer-events CSS property
    • How to disable HTML links

    The How to disable HTML links answer attached suggested using both disabled and pointer-events: none for maximum browser-support.

    a[disabled] {
        pointer-events: none;
    }
    

    Link to source: How to disable Link

    0 讨论(0)
  • 2020-12-14 06:19

    Based on nbeuchat's answer and component - I've created an own improved version of component that overrides react router's Link component for my project.

    In my case I had to allow passing an object to to prop (as native react-router-dom link does), also I've added a checking of search query and hash along with the pathname

    import PropTypes from 'prop-types';
    import React, { Component } from 'react';
    import { Link as ReactLink } from 'react-router-dom';
    import { withRouter } from "react-router";
    
    const propTypes = {
      to: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.object]),
      location: PropTypes.object,
      children: PropTypes.node,
      onClick: PropTypes.func,
      disabled: PropTypes.bool,
      staticContext: PropTypes.object
    };
    
    class Link extends Component {
      handleClick = (event) => {
        if (this.props.disabled) {
          event.preventDefault();
        }
    
        if (typeof this.props.to === 'object') {
          let {
            pathname,
            search = '',
            hash = ''
          } = this.props.to;
          let { location } = this.props;
    
          // Prepend with ? to match props.location.search
          if (search[0] !== '?') {
            search = '?' + search;
          }
    
          if (
            pathname === location.pathname
            && search === location.search
            && hash === location.hash
          ) {
            event.preventDefault();
          }
        } else {
          let { to, location } = this.props;
    
          if (to === location.pathname + location.search + location.hash) {
            event.preventDefault();
          }
        }
    
        // Ensure that if we passed another onClick method as props, it will be called too
        if (this.props.onClick) {
          this.props.onClick(event);
        }
      };
    
      render() {
        let { onClick, children, staticContext, ...restProps } = this.props;
        return (
          <ReactLink
            onClick={ this.handleClick }
            { ...restProps }
          >
            { children }
          </ReactLink>
        );
      }
    }
    
    Link.propTypes = propTypes;
    
    export default withRouter(Link);
    
    0 讨论(0)
  • 2020-12-14 06:20

    All the goodness of React Router NavLink with the disable ability.

    import React from "react"; // v16.3.2
    import { withRouter, NavLink } from "react-router-dom"; // v4.2.2
    
    export const Link = withRouter(function Link(props) {
      const { children, history, to, staticContext, ...rest } = props;
      return <>
        {history.location.pathname === to ?
          <span>{children}</span>
          :
          <NavLink {...{to, ...rest}}>{children}</NavLink>
        }
      </>
    });
    
    0 讨论(0)
  • 2020-12-14 06:29

    I think you should you atrribute to=null to set disable a link.

    See an example at here https://stackoverflow.com/a/44709182/4787879

    0 讨论(0)
  • 2020-12-14 06:31

    I'm not going to ask why you would want this behavior, but I guess you can wrap <Link /> in your own custom link component.

    <MyLink to="/foo/bar" linktext="Maybe a link maybe a span" route={this.props.route} />

    class MyLink extends Component {
        render () {
            if(this.props.route === this.props.to){
                return <span>{this.props.linktext}</span>
            }
            return <Link to={this.props.to}>{this.props.linktext}</Link>
        }
    }
    

    (ES6, but you probably get the general idea...)

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