Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How can I disable a <Link> in react-router, if its URL already active? E.g. if my URL wouldn't change on a click on <Link> I want to prevent clicking at all or render a <span> instead of a <Link>.

The only solution which comes to my mind is using activeClassName (or activeStyle) and setting pointer-events: none;, but I'd rather like to use a solution which works in IE9 and IE10.

like image 939
Pipo Avatar asked Mar 12 '16 20:03

Pipo


People also ask

How do I disable a link in react router?

Set the pointer events CSS property to none to disable a Link in React, e.g. <Link style={{pointerEvents: 'none'}}> . When the pointer events property of the link is set to none , the link is disabled. Copied!

How do I make a link inactive?

Disable a link #remove the href attribute so that it can no longer receive the focus. add a role="link" so that it is always considered a link by screen readers. add an attribute aria-disabled="true" so that it is indicated as being disabled.

How do you make a link not clickable in react JS?

You could just set set the link's onClick property: render () { return( <li> { this. props. notClickable ?

How do I restrict URL in react?

To restrict access to routes in React Router, we set the render prop to a function that renders the component we want according to the condition we're checking. import { Route, Redirect } from "react-router"; <Route exact path="/" render={() => (loggedIn ?


1 Answers

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

like image 51
Denis Bubnov Avatar answered Oct 02 '22 14:10

Denis Bubnov