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

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

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

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

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 :: react-router-dom 
Javascript :: how to install react router dom 
Javascript :: javascript get boolean if checkbox is checked 
Javascript :: jest mock react-redux hooks 
Javascript :: How to swap two array elements in JavaScript 
Javascript :: timestamp to date javascript 
Javascript :: javascript change hidden input value 
Javascript :: es6 map usin index 
Javascript :: jquery change input value if greater than 
Javascript :: read and update csv file in nodejs 
Javascript :: jquery animation 
Javascript :: how to make view dotted line in jsx 
Javascript :: node js variables in string 
Javascript :: ejs variable 
Javascript :: Program for factorial of a number in javascript 
Javascript :: reverse a number in javascript 
Javascript :: jquery sort listing alphabetically 
Javascript :: js addeventlistener hover 
Javascript :: jquery get selected dropdown item 
Javascript :: videojs cdn 
Javascript :: nodejs date difference 
Javascript :: js paste 
Javascript :: split string in angular 8 
Javascript :: jquery move element to another without losing events 
Javascript :: react run useeffect only once 
Javascript :: get status of a user discord js 
Javascript :: sessionstorage array 
Javascript :: regex is empty string javascript 
Javascript :: using index of on array of objects 
Javascript :: convert new date to minutes number javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =