Exercise
Exercise
1. Install Node.js:
o Make sure you have Node.js installed. You can check it by running node -v
and npm -v in the terminal.
o If not installed, go to Node.js and download the latest version.
“cd navbar-app”
“npm start”
This will start the development server and open the default React app in your browser.
Before we start, let's clean up some of the default files created by React to keep the project
simple.
1. Delete unnecessary files: In the src folder, you can delete the following files:
o App.test.js
o logo.svg
o reportWebVitals.js
o setupTests.js
2. Update App.css: Remove the default styles. You can add your own styles later.
function App() {
return (
<div className="App">
<h1>Welcome to My Navbar</h1>
</div>
);
}
1. Create a Navbar.js Component: In the src folder, create a new file called Navbar.js.
This component will contain the HTML structure of the navbar.
“
import React from 'react';
import './Navbar.css'; // Optional for styling later
“
import React from 'react';
import './App.css';
import Navbar from './Navbar';
function App() {
return (
<div className="App">
<Navbar />
<h1>Welcome to My Navbar</h1>
</div>
);
}
Now that we have a functioning navbar, let's add some basic CSS to style it.
1. Create a Navbar.css file inside the src folder:
“
.navbar {
background-color: #333;
color: white;
padding: 15px;
display: flex;
justify-content: space-between;
align-items: center;
}
.logo {
font-size: 24px;
font-weight: bold;
}
.nav-links {
list-style: none;
display: flex;
gap: 20px;
}
.nav-links a {
color: white;
text-decoration: none;
font-size: 18px;
}
.nav-links a:hover {
text-decoration: underline;
}
”
Add the following CSS to the Navbar.css file to make the navbar more responsive on
smaller screens.
“
@media (max-width: 600px) {
.nav-links {
flex-direction: column;
gap: 10px;
}
}
”
• Components: React components like Navbar allow us to break the UI into reusable
pieces.
• JSX: JSX combines JavaScript with HTML-like syntax, making it easier to structure
components.
• Styling: We used CSS to style the components and media queries to make the app
responsive.
• Props (Bonus): You can extend this example by passing props to the Navbar to make
it dynamic (e.g., pass a logo or diherent link sets).
Bonus Step (Optional): Add Active Link Highlighting
To add some interactivity, you can implement active link highlighting when a user clicks on
a particular section. This can be done using React state or libraries like react-router-dom
for navigation.