Categories
React Answers

How to disable a link in React?

Spread the love

To disable a link in React, we use some CSS styles.

For instance, we write

const C = (props) => {
  return (
    <li>
      {props.notClickable ? (
        <Link
          to="/"
          className="disabledCursor"
          onClick={(event) => event.preventDefault()}
        >
          Link
        </Link>
      ) : (
        <Link to="/" className="notDisabled">
          Link
        </Link>
      )}
    </li>
  );
};

to add Link components.

Then we event.preventDefault() to disable the default behavior when responding to clicks.

And we change the cursor when we hover over the disabled link with

.disabledCursor { 
  cursor: default;
}

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *