An online markdown blog and knowledge repository.
Key takeaways and code samples from the video.
Is a header with a site title and nav items in it.
<nav>
element enclosing an anchor tag to root for the title of the siteIn the top-level component (e.g. App) add window.location
to get the URL of the page that was navigated to.
Review Console output, Location member, to see properties href, origin, and pathname.
Using a switch statement on window.location.pathname
and set cases for each literal path:
import Navbar from "./Navbar.js";
import Home from "./pages/Home.js";
import Contentpage from "./pages/Contentpage.js";
import About from "./pages/About.js";
function App() {
let Component;
switch (window.location.pathname) {
case "/":
Component = Home;
break
case "/contentpage":
Component = Contentpage;
break
case "/about":
Component = About;
break
}
return (
<>
<Navbar />
<Component />
</>
)
}
Make the currently selected Nav item highlighted after clicking:
function CustomLink({href, children, ...props}) { return() }
const path = window.location.pathname
path === href
href={href} {...props}
{children}
Note: This logic will need to be replaced if using react-router-dom and useMatch
hook or useResolvedPath
hook.
Use UL and LI elements to create the set of links for the nav bar.
Add styling for nav elements:
.nav {
background: #color;
color: #opposite-color;
display: flex;
justify-content: space-between;
align-items: center; // try 'stretch' if alignment still isn't quite right
gap: 2rem; // space between
padding: 0 1rem; // space to edges
}
Prettify the links:
.nav ul {
padding: 0;
margin: 0;
list-style: none; // eliminate extra spacing and the bullet icon
display: flex;
gap: 1rem;
}
Style the nav anchor tag:
.nav a {
color: inherit;
text-decoration: none;
height: 100%; // ensure link is as tall as its container content area
display: flex;
align-items: center;
padding: .25rem;
}
Style the links so they change appearance when hovered vs. non-hovered:
.nav li.active {
background-color: #some-background-color; // when selected or tabbed changes the background color for contrast
}
.nav li:hover {
background-color: #different-background-color; // when hovered alters the background color for contrast
}
import { BrowserRouter } from 'react-router-dom';
<BrowserRouter>
open and close elements.<Navbar />
component in return fragment, include <Routes>
and <Route>
tags (see example code below).<Route path="" element="">
tags and attributes for each navigation route desired.<Link>
Component from react-router-dom;href
with to
anywhere an Anchor has been replaced with a Link component.function App() {
return (
<>
<Navbar />
<div className="container">
<Routes>
<Route path="/" element={ <Home /> } />
</Routes>
</div>
</>
);
}
useResolvedPath
in place of window.location.pathname
.to
property to the CustomLink function and bring it down to the useResolvedPath() parameter.useMatch({path: resolvedPath.pathname, end: true})
result to another variable within the CustomLink function.end: true
forces matching to occur through an entire path and may/not be necessary.Now only the Component that is navigated to re-renders fully, the rest of the page does not unless there are changees like :active and :hover.
An absolute path is one that describes the path location from the root.
A relative path describes only the page name itself, ignoring parent components of the full path.
Suggestions:
Return to conted-index.md