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

后端 未结 9 1027
谎友^
谎友^ 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: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 ?
          {children}
          :
          {children}
        }
      
    });
    

提交回复
热议问题