Skip to content

Week4 solutions#1

Open
bhas wants to merge 3 commits into
mainfrom
week4-solutions
Open

Week4 solutions#1
bhas wants to merge 3 commits into
mainfrom
week4-solutions

Conversation

@bhas
Copy link
Copy Markdown
Owner

@bhas bhas commented May 4, 2026

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. isDark and toggleTheme were previously passed as props from App to all the other components.

Remember when working with contexts we need to do 3 things:

  1. create the context in a new file with createContext(...)
  2. Provide the context using <MyContext.Provider value={...}> in the App.jsx
  3. Use the context in any component that needs the value by using useContext(...)

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:

  • Component libraries such as Material UI gives fully functional components for common UI needs out-of-the-box. It requires very little code to start using, but it can also be harder to customize to your desired look.
  • CSS frameworks such as TailwindCSS helps you style components easier and faster, but does not provide any new components. It is only meant to help you write CSS faster.
  • Building everything yourself is also fine as it gives you full control and reduced complexity of adding external packages. However, more often than not you will see significant development speed using one of the above.

Before vs After

3.1

image

3.2

image

3.3

image

3.4

image

3.6

image

<Navbar isDark={isDark} onToggle={toggleTheme} />
<Page isDark={isDark} />
</div>
<ThemeContext.Provider value={{ isDark, toggleTheme }}>
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I add toggleTheme as a part of the context so that components also have a way to change the theme

Comment on lines +24 to +29
<NavLink to="/about/team" className={subNavClass}>
Our Team
</NavLink>
<NavLink to="/about/mission" className={subNavClass}>
Our Mission
</NavLink>
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The 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

<Route path="mission" element={<Mission />} />
</Route>
<Route path="/contact" element={<Contact />} />
<Route path="*" element={<NotFound />} />
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Placing a <Route path="*" ...> at the end will match for any url that did not match one of the above routes.

Comment on lines +13 to +16
"@emotion/react": "^11.14.0",
"@emotion/styled": "^11.14.1",
"@mui/icons-material": "^9.0.0",
"@mui/material": "^9.0.0",
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +9 to +31
// 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>
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Material UI uses its own context to provide the theme for all UI components

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant