DekGenius.com
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}/>
router react
npm install react-router-dom
react router
//React Router 6
import Home from './Home';
import Navbar from './Navbar';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import Create from './Create';
import BlogDetails from './BlogDetails';
import NotFound from './NotFound';
function App() {
return (
<Router>
<div className="App">
<Navbar />
<div className="content">
<Routes>
<Route path="/" element={<Home />} />
<Route path="/create" element={<Create />} />
<Route path="/blogs/:id" element={<BlogDetails />} />
<Route path="*" element={<NotFound />} />
</Routes>
</div>
</div>
</Router>
);
}
export default App;
routes react
npm install --save react-router-dom
react router
// APP.js Setup.........
import * as React from "react";
import { BrowserRouter, Route, Routes } from "react-router-dom";
import "./App.css";
import Home from "./Component/Home/Home";
function App() {
return (
<div className="App">
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
</Routes>
</BrowserRouter>
</div>
);
}
export default App;
// index.js setup
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import "./index.css";
import reportWebVitals from "./reportWebVitals";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
reportWebVitals();
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>;
}
react router
import "./App.css";
import SignIn from "./SignIn";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import PrimarySearchAppBar from "./Header";
import SignUp from "./SignUp";
function App() {
return (
<Router>
<PrimarySearchAppBar />
<Routes>
{["/", "/home"].map((path) => (
<Route exact path={path} element={<Home />} />
))}
<Route path="/signin" exact element={<SignIn />} />
<Route path="/signup" exact element={<SignUp />} />
<Route exact path="*" element={<ComingSoon />} />
</Routes>
</Router>
);
}
export default App;
react router
import React from 'react';
import ReactDOM from 'react-dom';
import { Route, Link, BrowserRouter as Router } from 'react-router-dom'
import './index.css';
import App from './App';
import About from './about'
import Contact from './contact'
const routing = (
<Router>
<div>
<h1>React Router Example</h1>
<Route exact path="/" component={App} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
</div>
</Router>
)
ReactDOM.render(routing, document.getElementById('root'));
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>;
}
react router
react router
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
return (
<>
<Router>
<>
<Routes>
<>
<Route
exact
path="/"
element={
<>
<Dashboard allFaq={allFaq} />
</>
}
/>
<Route
exact
path="/login"
element={
<>
<Login />
</>
}
/>
</>
</Routes>
</>
</Router>
</>
);
react router
import ReactDOM from "react-dom";
import { Route, BrowserRouter as Router, Switch } from "react-router-dom";
const routing = (
<Router>
<React.StrictMode>
<Header />
<Switch>
<Route exact path="/" component={Register} />
<Route exact path="/Login" component={Login} />
<Route exact path="/Home" component={App} />
<Route path="/Logout" component={Logout} />
<Route path="/CreatePost" component={CreatePost} />
<Route path="/CreateComment" component={CreateComment} />
<Route path="/UserProfile" component={UserProfile} />
<Route path="/SearchProfile" component={SearchedProfile} />
<Route path="/follow" component={FollowDisplay} />
<Route path="/following" component={FollowingDisplay} />
</Switch>
<Footer />
</React.StrictMode>
</Router>
);
react router
<Routes>
<Route path="/" element={<Home />} />
<Route path="/books" element={<BookList />} />
<Route path="/books/:id" element={<Book />} />
</Routes>
react router
import ReactDOM from "react-dom/client";
import {
BrowserRouter,
Routes,
Route,
} from "react-router-dom";
// import your route components too
const root = ReactDOM.createRoot(
document.getElementById("root")
);
root.render(
<BrowserRouter>
<Routes>
<Route path="/" element={<App />}>
<Route index element={<Home />} />
<Route path="teams" element={<Teams />}>
<Route path=":teamId" element={<Team />} />
<Route path="new" element={<NewTeamForm />} />
<Route index element={<LeagueStandings />} />
</Route>
</Route>
</Routes>
</BrowserRouter>
);
react router
import React from "react";
import { Link, Route, Switch } from "react-router-dom";
import Category from "./Category";
import Products from "./Products";
import Login from "./Login";
import PrivateRoute from "./PrivateRoute";
import "./styles.css";
const Home = () => (
<div>
<h2>Home</h2>
</div>
);
const Admin = () => (
<div>
<h2>Welcome admin!</h2>
</div>
);
export default function App() {
return (
<div>
<nav className="navbar navbar-light">
<ul className="nav navbar-nav">
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/category">Category</Link>
</li>
<li>
<Link to="/products">Products</Link>
</li>
<li>
<Link to="/admin">Admin area</Link>
</li>
</ul>
</nav>
<Switch>
<Route exact path="/">
<Home />
</Route>
<Route path="/category">
<Category />
</Route>
<Route path="/login">
<Login />
</Route>
<Route path="/products">
<Products />
</Route>
<PrivateRoute path="/admin" component={Admin} />
</Switch>
</div>
);
}
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>
);
react router
npm install react-router-dom
# Documentation: https://reactrouter.com/
# Upgrading from prev. versions: https://reactrouter.com/docs/en/v6/upgrading/v5
react router
// npm install react-router-dom
// yarn add react-router-dom
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";
routing in react
npm install react-router-dom
to install the react router to create a specific urls in react
route in component react
import { BrowserRouter, Route, Link } from "react-router-dom";
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
....
}
react router
<Route path="/" element={<App />}>
<Route path="sales" element={<Sales />}>
<Route path="invoices" element={<Invoices />}>
<Route path=":invoice" element={<Invoice />} />
</Route>
</Route>
</Route>
router react
npm install -g create-react-app
create-react-app demo-app
cd demo-app
routing in react
npx create-react-app nameOfApplication
cd nameOfApplication
when we create the application we use this commands.
© 2022 Copyright:
DekGenius.com