//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}/>
npm install --save react-router-dom
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>;
}
// 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>;
}
import {
BrowserRouter as Router,
Routes,
Route,
Link
} from 'react-router-dom';
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
....
}
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>
);
npm install react-router-dom
to install the react router to create a specific urls in react
import { BrowserRouter, Route, Link } from "react-router-dom";
npx create-react-app nameOfApplication
cd nameOfApplication
when we create the application we use this commands.