How to disable a link in React Router 7 / Remix
Felipe Freitag Vargas

Felipe Freitag Vargas @felipefreitag

About: Founder at Seasoned, building apps since 2017 | Flashboard ⚡ Instant Admin Panels for PostgreSQL, Supabase, Neon, Replit, and more

Location:
Porto Alegre, RS, Brazil
Joined:
Jan 9, 2020

How to disable a link in React Router 7 / Remix

Publish Date: Jan 9
3 0

Today, I joined a discussion on the Remix Discord about how to block a Link component when a certain condition is true.

A simple solution is to use a ternary to render either the Link or a disabled button styled to look identical. You can abstract it into a reusable component. I've used this pattern at Seasoned many times 😁

{disabled ? (
  <button
    className="bt bt-blue" // add the disabled appearance as needed
    disabled={disabled}
  >
    <CheckIcon /> Go!
  </button>
) : (
  <Link
    to={`....`}
    className="bt bt-blue"
    preventScrollReset
  >
    <CheckIcon /> Go!
  </Link>
)
}
Enter fullscreen mode Exit fullscreen mode

Enjoy!

Comments 0 total

    Add comment