Skip to content

Commit 4ec5b72

Browse files
committed
fix: Refactor Navbar routing for React Router v7
1 parent 62612f6 commit 4ec5b72

File tree

4 files changed

+44
-61
lines changed

4 files changed

+44
-61
lines changed

website/package-lock.json

Lines changed: 8 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

website/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"react": "^19.1.0",
1111
"react-apexcharts": "^1.7.0",
1212
"react-dom": "^19.1.0",
13-
"react-router-dom": "^7.5.0",
13+
"react-router-dom": "^7.5.1",
1414
"react-scripts": "^5.0.1",
1515
"react-scroll": "^1.8.1",
1616
"recharts": "^2.0.8"

website/src/App.js

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,14 @@ function App() {
1515
return (
1616
<div className="app">
1717
<Router>
18-
{/* conditionally render the heading in navbar */}
19-
<Routes>
20-
{/* render default navbar if URL is empty */}
21-
<Route
22-
exact
23-
path="/"
24-
element={
25-
<div className="navbar">
26-
<Navbar path="/" subtitle={getHeader()} />
27-
</div>
28-
}
29-
></Route>
30-
{/* pass in URL through params if it's not the default URL */}
31-
<Route
32-
path="/:currentPage"
33-
render={(input) => (
34-
<div className="navbar">
35-
<Navbar
36-
path={"/" + input.match.params.currentPage}
37-
subtitle={getHeader(input.match.params.currentPage)}
38-
/>
39-
</div>
40-
)}
41-
></Route>
42-
</Routes>
18+
<div className="navbar">
19+
<Routes>
20+
<Route path="/">
21+
<Route index element={<Navbar />} />
22+
<Route path=":currentPage" element={<Navbar />} />
23+
</Route>
24+
</Routes>
25+
</div>
4326

4427
<Routes>
4528
<Route exact path="/" element={<LandingPage />} />
@@ -56,21 +39,3 @@ function App() {
5639
}
5740

5841
export default App;
59-
60-
//function that renders the heading for the navbar from the route parameters
61-
function getHeader(path) {
62-
let subtitle = "A TeachLA Learning Lab!";
63-
const pageNames = {
64-
aboutUs: "Who's TeachLA?",
65-
college: "Case Study #1: College Admissions",
66-
facebook: "Case Study #2: Facebook Ads",
67-
facialRecognition: "Case Study #3: Facial Recognition",
68-
conclusion: "In Conclusion...",
69-
resources: "To Learn More...",
70-
};
71-
//return default dictionary value if path is falsy
72-
73-
//get the subtitle from our pageNames dictionary if path exists
74-
if (path) ({ [path]: subtitle } = pageNames);
75-
return subtitle;
76-
}

website/src/components/mainContent/Navbar.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import React, { useState } from "react";
2-
import { Link } from "react-router-dom";
2+
import { Link, useParams } from "react-router-dom";
33
import teachLogo from "../../assets/teachla-logo.svg";
44
import navButton from "../../assets/navButton.svg";
55
import "../../main.css";
66
import "../../index.css";
77
import "./navbar.css";
88

99
export default function NavBar(props) {
10+
const params = useParams();
11+
1012
// dropdownOpen: boolean variable for whether the dropdown menu is open or closed
1113
const [dropdownOpen, setDropdownOpen] = useState(false);
1214

@@ -15,6 +17,11 @@ export default function NavBar(props) {
1517
setDropdownOpen(!dropdownOpen);
1618
};
1719

20+
// on the homepage (/), currentPage is intentionally undefined
21+
const currentPage = params.currentPage;
22+
23+
const subtitle = getHeader(currentPage);
24+
1825
const navSections = [
1926
{
2027
path: "/",
@@ -94,9 +101,27 @@ export default function NavBar(props) {
94101
<Link className="title-link" to="/">
95102
<h1 className="title">Bias by Us</h1>
96103
</Link>
97-
<h2 className="subtitle">{props.subtitle}</h2>
104+
<h2 className="subtitle">{subtitle}</h2>
98105
</div>
99106
{dropdownArea()}
100107
</div>
101108
);
102109
}
110+
111+
//function that renders the heading for the navbar from the route parameters
112+
function getHeader(path) {
113+
let subtitle = "A TeachLA Learning Lab!";
114+
const pageNames = {
115+
aboutUs: "Who's TeachLA?",
116+
college: "Case Study #1: College Admissions",
117+
facebook: "Case Study #2: Facebook Ads",
118+
facialRecognition: "Case Study #3: Facial Recognition",
119+
conclusion: "In Conclusion...",
120+
resources: "To Learn More...",
121+
};
122+
//return default dictionary value if path is falsy
123+
124+
//get the subtitle from our pageNames dictionary if path exists
125+
if (path) ({ [path]: subtitle } = pageNames);
126+
return subtitle;
127+
}

0 commit comments

Comments
 (0)