Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

custom link react

//Simple example
import { Link, LinkProps, useMatch, NavLink, useResolvedPath } from "react-router-dom";//v6.0

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

  return (
    <div>
      <Link className={match ? "nav-active" : ""} {...props} to={resolved.pathname}>
        {children}
      </Link>
    </div>
  );
};


// How to use here
import { CustomLink } from "./components/CustomLink";

  function App() {
    return (
      <>
        <div className="main-layout">
          <nav>
            <img src={logo} alt="Logo" />

            <ul>
              <li>
                <CustomLink to="/">Home</CustomLink>
              </li>
              <li>
                <CustomLink to="/about">About</CustomLink>
              </li>
              <li>
                <CustomLink to="/users">Users</CustomLink>
              </li>
            </ul>
          </nav>

          <Routes>
            <Route path="/about" element={<h1>About Page</h1>} />
            <Route path="/users" element={<h1>Users Page</h1>} />
            <Route path="/" element={<h1>Home Page</h1>} />

            <Route path="/*" element={<Navigate to="/" replace />} />
          </Routes>
        </div>
      </>
    );
}


//Or more Simple

function App() {
  return (
    <>
      <div className="main-layout">
        <nav>
          <img src={logo} alt="Logo" />
          <ul>
            <li>
              <NavLink className={({ isActive }) => (isActive ? "nav-active" : "")}
                to="/users"
              >
                Users
              </NavLink>
            </li>
          </ul>
        </nav>

        <Routes>
          <Route path="/users" element={<h1>Users Page</h1>} />
          <Route path="/*" element={<Navigate to="/" replace />} />
        </Routes>
      </div>
    </>
  );
}


Comment

PREVIOUS NEXT
Code Example
Typescript :: avatar image mui not centered 
Typescript :: 2. Write a program to draw this. Assume the innermost square is 20 units per side, and each successive square is 20 units bigger, per side, than the one inside it. 
Typescript :: gamemanager unity resets after reloading scene 
Typescript :: flutter web keep focus on textfield 
Typescript :: salesforce lwc data binding for multiple inputs values 
Typescript :: how to add multiple inputs to a dictionary python 
Typescript :: how to Write a program that accepts three decimal numbers as input and outputs their sum on python 
Typescript :: preventing letters from being placed in an input ts 
Typescript :: get and set in typescript 
Typescript :: how to remove second square brackets in an array 
Typescript :: typescript array 
Typescript :: charts flutter 
Typescript :: change field name relation typeorm 
Typescript :: print all alphabets from a to z in java 
Typescript :: mailbox exists c# 
Typescript :: different types of errors in numerical methods 
Typescript :: 2 positional arguments but 3 were given 
Typescript :: Angular 8 ngClass If 
Typescript :: how to reset windows update components in windows 
Typescript :: nuxt "AxiosRequestConfig" 
Typescript :: nuxt 3 nuxtServerInit 
Typescript :: date formats in mongodb 
Typescript :: typescript document.getelementbyid object is possibly null 
Typescript :: ts remainder of Division 
Typescript :: curl -s "http://google.com?[1-1000]" 
Typescript :: W/TextToSpeech: speak failed: not bound to TTS engine site:stackoverflow.com 
Typescript :: This program prompts the user for two numbers, calls a function to determine the smaller number and prints the smaller number that is returned from the function 
Typescript :: Could not resolve all artifacts for configuration 
Typescript :: knex could not determine data type of parameter $1 
Typescript :: nativescript routerextensions navigate 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =