26
loading...
This website collects cookies to deliver better user experience
import React from 'react';
import { Route, Switch } from 'react-router-dom';
import Home from 'path-to-home';
import Cart from 'path-to-cart';
<Switch>
<Route exact path="/"
<Home/>
</Route>
<Route path="/cart"
<Cart/>
</Route>
</Switch>
<Rote/>
, now we need a pass as a prop using this new tag called element
.exact
doesn't necessary, and an order from routes doesn't matter anymore.import React from 'react';
import { Route, Routes } from 'react-router-dom';
import Home from 'path-to-home';
import Cart from 'path-to-cart';
<Routes>
<Route path="/" element={ <Home/> } />
<Route path="/cart" element={ <Cart/> } />
</Routes>
<Routes/>
.// app.jsx
import React from 'react';
import { Route, Routes, Navegate } from 'react-router-dom';
<Routes>
<Route path="/" element={ <Navegate _replace_ to="/welcome" /> }>
<Route path="/welcome/*" element={ <Welcome/> } >
<Route path="new-user" element={ <p>Welcome, new user!</p> } />
</Route>
<Route path="/cart" element={ <Cart/> } />
</Routes>
replace
on <Navegate>
is optional to use, but semantic correct when used to replace pages. <Outlet/>
to indicate the correct position on the nested page as you can see on the code below:// Welcome.jsx
import React from 'react';
import { Link, Outlet } from 'react-router-dom';
const Welcome () => {
return (
<section>
<h1> This is you particular space</h1>
<Outlet />
<Link to="/home"> Return to Home <Link/>
</section>
);
};
<Route children>
from the v5 version changed to <Route element>
in v6. This should be as simple as moving your route element from the child position to a named element prop.import { NavLink } from "react-router-dom";
<NavLink activeClassName= { classes.active } to='/home' >
Home
</NavLink>
<Link/>
and <NavLink/>
nothing change with then. However, what did change was the activeClassName props, it doesn't existimport { NavLink } from "react-router-dom";
<NavLink className= {
(thisNav) => thisNav.isActive ? class.active : ''}
to='/home'>
Home
</NavLink>
{ useHistory }
yet, even so, this hook is used to navigate between pages history.{ useNavigate }
, to turns the code more legible, or clean.