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.
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!
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.
You could just set set the link's onClick property: render () { return( <li> { this. props. notClickable ?
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 ?
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With