-
Notifications
You must be signed in to change notification settings - Fork 2
Week4 solutions #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { createContext } from 'react'; | ||
|
|
||
| // We provide the toggleTheme function that components can call to change the theme, but we don't implement it here. | ||
| // It is provided by the App | ||
| export const ThemeContext = createContext({ isDark: false, toggleTheme: () => {} }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,36 @@ | ||
| import { Routes, Route } from 'react-router-dom'; | ||
| import Navbar from './components/Navbar.jsx'; | ||
| import Home from './pages/Home.jsx'; | ||
| import About from './pages/About.jsx'; | ||
| import Contact from './pages/Contact.jsx'; | ||
| import Team from './pages/Team.jsx'; | ||
| import Mission from './pages/Mission.jsx'; | ||
| import './index.css'; | ||
|
|
||
| function NotFound() { | ||
| return ( | ||
| <main className="page"> | ||
| <h1 className="page__title">404 — Page not found</h1> | ||
| <p className="page__body">The page you're looking for doesn't exist.</p> | ||
| </main> | ||
| ); | ||
| } | ||
|
|
||
| export default function App() { | ||
| return ( | ||
| <div className="app"> | ||
| {/* We keep the Navbar outside of the Routes component because we want it to be visible on all pages, regardless of the current route. */} | ||
| <Navbar /> | ||
| <Home /> | ||
| <About /> | ||
| <Contact /> | ||
| {/* The Routes component is where we define all the routes for our application. So the component will render here. */} | ||
| <Routes> | ||
| <Route path="/" element={<Home />} /> | ||
| <Route path="/about" element={<About />}> | ||
| <Route path="team" element={<Team />} /> | ||
| <Route path="mission" element={<Mission />} /> | ||
| </Route> | ||
| <Route path="/contact" element={<Contact />} /> | ||
| <Route path="*" element={<NotFound />} /> | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Placing a |
||
| </Routes> | ||
| </div> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,12 @@ | ||
| import { StrictMode } from 'react' | ||
| import { createRoot } from 'react-dom/client' | ||
| import { BrowserRouter } from 'react-router-dom' | ||
| import App from './App.jsx' | ||
|
|
||
| createRoot(document.getElementById('root')).render( | ||
| <StrictMode> | ||
| <App /> | ||
| <BrowserRouter> | ||
| <App /> | ||
| </BrowserRouter> | ||
| </StrictMode>, | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,11 @@ | ||
| import { NavLink, Outlet } from 'react-router-dom'; | ||
|
|
||
| function subNavClass({ isActive }) { | ||
| // isActive is a boolean from NavLink that tells us if the link is active. | ||
| // we use it to conditionally apply the 'active' class to the link, which will style it differently when it's active. | ||
| return isActive ? 'about__sublink active' : 'about__sublink'; | ||
| } | ||
|
|
||
| export default function About() { | ||
| return ( | ||
| <main className="page"> | ||
|
|
@@ -13,11 +21,17 @@ export default function About() { | |
| </ul> | ||
|
|
||
| <nav className="about__subnav"> | ||
| <a href="#" className="about__sublink">Our Team</a> | ||
| <a href="#" className="about__sublink">Our Mission</a> | ||
| <NavLink to="/about/team" className={subNavClass}> | ||
| Our Team | ||
| </NavLink> | ||
| <NavLink to="/about/mission" className={subNavClass}> | ||
| Our Mission | ||
| </NavLink> | ||
|
Comment on lines
+24
to
+29
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can freely put NavLinks and Links wherever we want on our page. So we can have several menus and sub menus |
||
| </nav> | ||
|
|
||
| {/* Render the active nested page here */} | ||
| {/* The Outlet is where the nested routes will be rendered. | ||
| So when we navigate to /about/team, the Team component will be rendered here, and when we navigate to /about/mission, the Mission component will be rendered here. */} | ||
| <Outlet /> | ||
| </main> | ||
| ); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I add
toggleThemeas a part of the context so that components also have a way to change the theme