Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react routing

//First install react router
npm install react-router-dom

//Import it from your react index.js file
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';

//Rap your app with BrowserRouter
<BrowserRouter>
	<App />  
</BrowserRouter>

//Define you Routes in your app and import it on the top
<Routes>
  <Route path="/" element={<Home />} />
  <Route path="/create" element={<Create />} />
  <Route path="/blogs/:id" element={<BlogDetails />} />
  <Route path="*" element={<NotFound />} />
</Routes>

//Now you are all set. You can use this with
<Link to="/create" element={Create}/>
Comment

routes react

npm install --save react-router-dom
Comment

what is router in react

React router as a browserRouter in App.js for navbar links like Home,
services,conatct-us etc.
   // npm install react-router-dom

// ##### Basic Routing #####
import React from 'react';
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';

export default function App() {
   return (
      <Router>
         <div>
            <nav>
               <ul>
                  <li>
                     <Link to="/">Home</Link>
                  </li>
                  <li>
                     <Link to="/about">About</Link>
                  </li>
                  <li>
                     <Link to="/users">Users</Link>
                  </li>
               </ul>
            </nav>

            {/* A <Switch> looks through its children <Route>s and
            renders the first one that matches the current URL. */}
            <Switch>
               <Route path="/about">
                  <About />
               </Route>
               <Route path="/users">
                  <Users />
               </Route>
               <Route path="/">
                  <Home />
               </Route>
            </Switch>
         </div>
      </Router>
   );
}

function Home() {
   return <h2>Home</h2>;
}

function About() {
   return <h2>About</h2>;
}

function Users() {
   return <h2>Users</h2>;
}
Comment

react routing

// installing dependency
yarn add react-router-dom

// implementation
import React from "react";
import {
  BrowserRouter as Router,
  Switch,
  Route,
  Link
} from "react-router-dom";

export default function App() {
  return (
    <Router>
      <div>
        <nav>
          <ul>
            <li>
              <Link to="/">Home</Link>
            </li>
            <li>
              <Link to="/about">About</Link>
            </li>
            <li>
              <Link to="/users">Users</Link>
            </li>
          </ul>
        </nav>

        {/* A <Switch> looks through its children <Route>s and
            renders the first one that matches the current URL. */}
        <Switch>
          <Route path="/about">
            <About />
          </Route>
          <Route path="/users">
            <Users />
          </Route>
          <Route path="/">
            <Home />
          </Route>
        </Switch>
      </div>
    </Router>
  );
}

function Home() {
  return <h2>Home</h2>;
}

function About() {
  return <h2>About</h2>;
}

function Users() {
  return <h2>Users</h2>;
}
Comment

router in react

import {
    BrowserRouter as Router,
    Routes,
    Route,
    Link
} from 'react-router-dom';
Comment

react routing

import React, { Suspense, lazy } from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';

const Home = lazy(() => import('./routes/Home'));
const About = lazy(() => import('./routes/About'));

import Shop from './routes/shop/shop';

const App = () => (
  <Router>
      <Routes>
<Route path='/' element={<Navigation />}>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
        <Route path='shop/*' element={<Shop />} />
</Route>
      </Routes>
  </Router>
);

#in navigation.js
const Navigation = () => {
	return (
    <>
      <Whatever />
       
      <Outlet /> #important
    </>
  );
};

#in shop.js
import { Routes, Route } from 'react-router-dom';
const Shop = () => {
  return (
    <Routes>
      <Route index element={<CategoriesPreview />} />
      <Route path=':category' element={<Category />} />
    </Routes>
  );
};

#in category.js
import { useParams } from 'react-router-dom';
const Category = () => {
  const { category } = useParams(); #destructure the param
  ....
}
Comment

React route

let routes = (
 <BrowserRouter>
      <Navbar />
      <div className="container mt-2" style={{ marginTop: 40 }}>
        <Switch>
          <Route exact path="/">
            <Home />
          </Route>
          <Route path="/about">
            <About />
          </Route>
        </Switch>
      </div>
    </BrowserRouter>
);
Comment

routing in react

npm install react-router-dom
to install the react router to create a specific urls in react
Comment

route in component react

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

routing in react

npx create-react-app nameOfApplication
cd nameOfApplication
when we create the application we use this commands.
Comment

PREVIOUS NEXT
Code Example
Javascript :: db.each store rowa 
Javascript :: javascript firestore autoID 
Javascript :: leaflet-src.js?e11e:4066 Uncaught (in promise) Error: Map container not found 
Javascript :: js match emoticon 
Javascript :: Differences between detach(), hide() and remove() - jQuery 
Javascript :: javascript check if json object is valid 
Javascript :: jest visit a page 
Javascript :: show mwssage js 
Javascript :: run strapi plugin at startup 
Javascript :: angular chart js Doughnut colors 
Javascript :: tskill nodejs port 
Javascript :: how to acces db knex 
Javascript :: how to push into an array javascript 
Javascript :: jhipster success alert 
Javascript :: Insert javascript variable into html string 
Javascript :: appscript json manifest chat 
Javascript :: how to send authorization in header of HTTP GET using ajax 
Javascript :: CFBundleShortVersionString in app.json 
Javascript :: cli run js 
Javascript :: aos library slow animation angular 
Javascript :: react native bordered image drop with shadow fix 
Javascript :: var relnojudul = 0; 
Javascript :: LogRocket 
Javascript :: advantage of array 
Javascript :: FORM EN JAVA SCRIPT 
Javascript :: get the first value when mapping through the array 
Javascript :: locak storage for table js 
Javascript :: eslint globals "_" true lodash 
Javascript :: load bmfont three with webpack 
Javascript :: how to call javascript method using selectlist on change in vf page 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =