Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to make react router Link active

const Router = () => (
  <BrowserRouter>
    <div>
      <Nav>
        <NavLink exact={true} activeClassName='is-active' to='/'>Home</NavLink>
        <NavLink activeClassName='is-active' to='/about'>About</NavLink>
      </Nav>

      <Match pattern='/' exactly component={Home} />
      <Match pattern='/about' exactly component={About} />
      <Miss component={NoMatch} />
    </div>
  </BrowserRouter>
)
Comment

react-router-dom navlink active

<NavLink
  to="users"
  className={({ isActive }) => (isActive ? 'active' : 'inactive')}
>
  Users
</NavLink>
Comment

react router active link

// react router v6 active route
import * as React from "react";
import {
  Routes,
  Route,
  Outlet,
  Link,
  useMatch,
  useResolvedPath,
} from "react-router-dom";
import type { LinkProps } from "react-router-dom";

export default function App() {
  return (
    <div>
      <h1>Custom Link Example</h1>

      <p>
        This example demonstrates how to create a custom{" "}
        <code>&lt;Link&gt;</code> component that knows whether or not it is
        "active" using the low-level <code>useResolvedPath()</code> and
        <code>useMatch()</code> hooks.
      </p>

      <Routes>
        <Route path="/" element={<Layout />}>
          <Route index element={<Home />} />
          <Route path="about" element={<About />} />
          <Route path="*" element={<NoMatch />} />
        </Route>
      </Routes>
    </div>
  );
}

function CustomLink({ children, to, ...props }: LinkProps) {
  let resolved = useResolvedPath(to);
  let match = useMatch({ path: resolved.pathname, end: true });

  return (
    <div>
      <Link
        style={{ textDecoration: match ? "underline" : "none" }}
        to={to}
        {...props}
      >
        {children}
      </Link>
      {match && " (active)"}
    </div>
  );
}

function Layout() {
  return (
    <div>
      <nav>
        <ul>
          <li>
            <CustomLink to="/">Home</CustomLink>
          </li>
          <li>
            <CustomLink to="/about">About</CustomLink>
          </li>
        </ul>
      </nav>

      <hr />

      <Outlet />
    </div>
  );
}

function Home() {
  return (
    <div>
      <h1>Home</h1>
    </div>
  );
}

function About() {
  return (
    <div>
      <h1>About</h1>
    </div>
  );
}

function NoMatch() {
  return (
    <div>
      <h1>Nothing to see here!</h1>
      <p>
        <Link to="/">Go to the home page</Link>
      </p>
    </div>
  );
}
Comment

react-active link

<NavLink
  to="/faq"
  style={isActive => ({
    color: isActive ? "green" : "blue"
  })}
>
  FAQs
</NavLink>
Comment

react router active link css

<NavLink
          key={tab.id}
          className={(navData) =>
            navData.isActive ? styles.activeTab : styles.inactiveTab
          }
          to={tab.path}
        >
          {tab.name}
        
</NavLink>
Comment

PREVIOUS NEXT
Code Example
Javascript :: como saber si un checkbox esta seleccionado en jquery 
Javascript :: javascript check date greater than today 
Javascript :: jquery trigger link click 
Javascript :: reactjs app change port 
Javascript :: A form label must be associated with a control react 
Javascript :: bind and unbind jquery validation 
Javascript :: how to access child img src in jquery 
Javascript :: check if a class exists javascript 
Javascript :: javascript redirect to url 
Javascript :: redirect page using javascript 
Javascript :: redirect with javascript to another page 
Javascript :: vehicle number yup validation 
Javascript :: getelementsbyname 
Javascript :: js test if i am in iframe 
Javascript :: javascript download string as file 
Javascript :: nested destructuring javascript 
Javascript :: dont trigger useeffect on start 
Javascript :: create subcollection firestore 
Javascript :: flutter text with icon 
Javascript :: show hide boxes using radio button selection jquery 
Javascript :: angular filter array of objects 
Javascript :: add commas to a number javascript 
Javascript :: compress string javascript 
Javascript :: javascript select all divs with class 
Javascript :: run a loop inside a console.log no blank line 
Javascript :: pagination jsonplaceholder 
Javascript :: loopback model properties default 
Javascript :: http get request js 
Javascript :: summation of array elements 
Javascript :: js check if date is future 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =