Week4 solutions#1
Conversation
| <Navbar isDark={isDark} onToggle={toggleTheme} /> | ||
| <Page isDark={isDark} /> | ||
| </div> | ||
| <ThemeContext.Provider value={{ isDark, toggleTheme }}> |
There was a problem hiding this comment.
Here I add toggleTheme as a part of the context so that components also have a way to change the theme
| <NavLink to="/about/team" className={subNavClass}> | ||
| Our Team | ||
| </NavLink> | ||
| <NavLink to="/about/mission" className={subNavClass}> | ||
| Our Mission | ||
| </NavLink> |
There was a problem hiding this comment.
We can freely put NavLinks and Links wherever we want on our page. So we can have several menus and sub menus
| <Route path="mission" element={<Mission />} /> | ||
| </Route> | ||
| <Route path="/contact" element={<Contact />} /> | ||
| <Route path="*" element={<NotFound />} /> |
There was a problem hiding this comment.
Placing a <Route path="*" ...> at the end will match for any url that did not match one of the above routes.
| "@emotion/react": "^11.14.0", | ||
| "@emotion/styled": "^11.14.1", | ||
| "@mui/icons-material": "^9.0.0", | ||
| "@mui/material": "^9.0.0", |
There was a problem hiding this comment.
Running npm install <package-name> for instance npm install @mui/material @emotion/react @emotion/styled will automatically add these lines to package.json so you don't have to modify the file yourself.
| // Bonus: override MUI's default primary colour | ||
| const theme = createTheme({ | ||
| palette: { | ||
| primary: { main: '#6c63ff' }, | ||
| }, | ||
| }); | ||
|
|
||
| export default function App() { | ||
| return ( | ||
| <div> | ||
| <header className="page-header"> | ||
| <h1>Week 4 — MUI Component Exercises</h1> | ||
| <p>Install MUI and replace each set of HTML elements with the MUI equivalents listed in the comments.</p> | ||
| </header> | ||
| <ContactForm /> | ||
| <ProductFilters /> | ||
| <ProfileCard /> | ||
| <ReviewForm /> | ||
| {/* Bonus: <CartItems /> */} | ||
| </div> | ||
| <ThemeProvider theme={theme}> | ||
| <CssBaseline /> | ||
| <div> | ||
| <header className="page-header"> | ||
| <h1>Week 4 — MUI Component Exercises</h1> | ||
| <p>Install MUI and replace each set of HTML elements with the MUI equivalents listed in the comments.</p> | ||
| </header> | ||
| <ContactForm /> | ||
| <ProductFilters /> | ||
| <ProfileCard /> | ||
| <ReviewForm /> | ||
| <CartItems /> | ||
| </div> | ||
| </ThemeProvider> |
There was a problem hiding this comment.
Material UI uses its own context to provide the theme for all UI components
This PR contains the solutions to the exercises we did during our React week 4 session May 4th.
Exercise 1 - Context
Refactored the theme system to remove prop drilling.
isDarkandtoggleThemewere previously passed as props fromAppto all the other components.Remember when working with contexts we need to do 3 things:
createContext(...)<MyContext.Provider value={...}>in theApp.jsxuseContext(...)Exercise 2 - Routing
Added React Router so each page lives at its own URL. The navbar links and sub-nav links are all functional, and the active link is highlighted.
Remember:
<NavLink>is used when we want custom styling the menu indicating the active page<Link>when we don't need any custom styling.<a>technically works too, but it causes the entire page to load which is both slow and makes us loose all temporary state such as values in fields.Exercise 3 - Material UI
Replaced all plain HTML form elements and layout primitives with MUI components across the application. Also includes both bonus tasks.
Remember:
Before vs After
3.1
3.2
3.3
3.4
3.6