Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react router dom v6 syntax

import './App.css';
import { Routes, Route } from "react-router-dom";

import {BrowserRouter} from 'react-router-dom'

function App() {
  return (
    < >
<BrowserRouter>   
    <Naavbar/>

    <NoteState>
  <Routes>
      <Route path="/" element={<Home />}/>
        <Route exact path="/about" element={<About />} />
      
    </Routes>

    </NoteState>
    </BrowserRouter>

  </>
  );
}

export default App;
 



Comment

react router dom v6

import {BrowserRouter,Routes,Route,Navigate,Outlet} from "react-router-dom";
<BrowserRouter>
    <Routes>
        <Route path="/" element={<Navigate to="/main" />} />
        <Route path="/home/*" element={<div><Outlet/></div>}>
            <Route path="page1" element={<h1>home page 1</h1>} />
            <Route path="page2" element={<h1>home page 2</h1>} />
        </Route>
        <Route path="/notification" element={<h1>notification page</h1>} />
        <Route path="/notification/:id" element={<h1>notification details pages</h1>} />
        <Route path="/login" element={<h1> login page </h1>} />
    </Routes>
</BrowserRouter>
Comment

react router v6

import React from 'react';
import ReactDOM from "react-dom";
import {
  BrowserRouter,
  Routes,
  Route,
} from "react-router-dom";
import App from "./App";

ReactDOM.render(
  <BrowserRouter>
    <Routes>
      <Route path="/" element={<App />} />
    </Routes>
  </BrowserRouter>,
  document.getElementById("root");
);
Comment

react router dom v6

//active route --> react router dom@v6
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 router dom v6

import { BrowserRouter, Routes, Route } from "react-router-dom";

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/signup" element={<SignUp />} />
        <Route path="/login" element={<Login />} />
      </Routes>
    </BrowserRouter>
  );
}
export default App;
Comment

react router v6

$ npm install history react-router-dom@next
Comment

react router v6

<HashRouter> 
    <Routes>
        <Route path='/app'> {/* put url base here and nest children routes */}
            <Route path='path1' element={ <Somecomponent1 /> } />
            <Route path='path2' element={ <Somecomponent2 /> } />
        </Route>
        <Route path="/*" element={<Navigate to="/app/path1" />}  /> {/* navigate to default route if no url matched */}
    </Routes>
</HashRouter>
Comment

react router dom v6

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>
  );
}
1
Comment

react router dom v6

React router dom setup v6
Comment

PREVIOUS NEXT
Code Example
Javascript :: The anchorEl prop provided to the component is invalid. 
Javascript :: js socket.emit 
Javascript :: sum index of an array javascript 
Javascript :: auto scroll to view react-native 
Javascript :: angular random number between 1 and 10 
Javascript :: js nuxt read/set cookie 
Javascript :: react router link with params 
Javascript :: javascript check date is greater than today 
Javascript :: jsjs trigger window error 
Javascript :: js get each pair of values from an array of objects 
Javascript :: Get specific route vuejs 
Javascript :: Material-ui Accessible icon 
Javascript :: Vue JS Production mode refresh causing 404 error 
Javascript :: js html input limit to 5 words 
Javascript :: child_process npm 
Javascript :: String variable props 
Javascript :: code intialization problem javascript 
Javascript :: Appending the option element using jquery each function 
Javascript :: curl send json as variable 
Javascript :: es6 array to object keys 
Javascript :: mongoose bulk create 
Javascript :: js replace 
Javascript :: vue dynamic routes with parameters 
Javascript :: js array random 
Javascript :: extract domain from url js 
Javascript :: js group by 
Javascript :: Javascript format date / time 
Javascript :: js group objects in array 
Javascript :: force delete and restore in sequelize 
Javascript :: make an arry from a string 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =