Create dynamic react navbar
- User
Create dynamic react navbar
- Assistant
Create dynamic react navbar
Creating a dynamic navigation bar (navbar) in React allows you to build a responsive and interactive user interface that adapts to user interactions and application state. Below is a step-by-step guide to creating a dynamic navbar using React.
First, create a new React project. You can use Create React App for a quick setup:
npx create-react-app dynamic-navbar
cd dynamic-navbar
Start the development server:
npm start
Your React application should now be running at http://localhost:3000/.
Create a new file Navbar.js inside the src directory:
src/
├── App.js
├── index.js
└── Navbar.js // New file
In Navbar.js, create a functional component:
// Navbar.js
import React from 'react';
function Navbar() {
return (
<nav>
<h2>My Website</h2>
<div>
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/contact">Contact</a>
</div>
</nav>
);
}
export default Navbar;
Import and use the Navbar component in App.js:
// App.js
import React from 'react';
import Navbar from './Navbar';
function App() {
return (
<>
<Navbar />
{/* Other components */}
</>
);
}
export default App;
Let's make the navbar dynamic by adding the following features:
First, add state to manage the visibility of the menu links on smaller screens.
// Navbar.js
import React, { useState } from 'react';
function Navbar() {
const [isMobile, setIsMobile] = useState(false);
return (
<nav>
<h2>My Website</h2>
<div className={isMobile ? 'nav-links-mobile' : 'nav-links'}
onClick={() => setIsMobile(false)}>
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/contact">Contact</a>
<a href="/login">Login</a>
</div>
<button className="mobile-menu-icon"
onClick={() => setIsMobile(!isMobile)}>
{isMobile ? '✖' : '☰'}
</button>
</nav>
);
}
export default Navbar;
Use React Router's NavLink component to manage active links. First, install react-router-dom:
npm install react-router-dom
Update Navbar.js:
// Navbar.js
import React, { useState } from 'react';
import { NavLink } from 'react-router-dom';
function Navbar() {
// ...existing code
return (
<nav>
<h2>My Website</h2>
<div className={isMobile ? 'nav-links-mobile' : 'nav-links'}
onClick={() => setIsMobile(false)}>
<NavLink exact to="/">Home</NavLink>
<NavLink to="/about">About</NavLink>
<NavLink to="/contact">Contact</NavLink>
<NavLink to="/login">Login</NavLink>
</div>
<button className="mobile-menu-icon"
onClick={() => setIsMobile(!isMobile)}>
{isMobile ? '✖' : '☰'}
</button>
</nav>
);
}
export default Navbar;
Wrap your App component with BrowserRouter in index.js:
// index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import App from './App';
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById('root')
);
Assuming you have authentication logic, use state or context to manage the user's authentication status. Here's a simplified example:
// Navbar.js
import React, { useState } from 'react';
import { NavLink } from 'react-router-dom';
function Navbar() {
const [isMobile, setIsMobile] = useState(false);
const [isAuthenticated, setIsAuthenticated] = useState(false); // Simplified
return (
<nav>
<h2>My Website</h2>
<div className={isMobile ? 'nav-links-mobile' : 'nav-links'}
onClick={() => setIsMobile(false)}>
<NavLink exact to="/">Home</NavLink>
<NavLink to="/about">About</NavLink>
<NavLink to="/contact">Contact</NavLink>
{isAuthenticated ? (
<NavLink to="/logout" onClick={() => setIsAuthenticated(false)}>Logout</NavLink>
) : (
<NavLink to="/login" onClick={() => setIsAuthenticated(true)}>Login</NavLink>
)}
</div>
<button className="mobile-menu-icon"
onClick={() => setIsMobile(!isMobile)}>
{isMobile ? '✖' : '☰'}
</button>
</nav>
);
}
export default Navbar;
Create a Navbar.css file for styling:
/* Navbar.css */
nav {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
background-color: #333;
padding: 1rem;
}
h2 {
color: #fff;
}
.nav-links, .nav-links-mobile {
display: flex;
list-style: none;
}
.nav-links a, .nav-links-mobile a {
color: #fff;
padding: 0 1rem;
text-decoration: none;
}
.nav-links-mobile {
display: none;
}
.mobile-menu-icon {
display: none;
font-size: 2rem;
color: #fff;
background: none;
border: none;
}
@media (max-width: 768px) {
.nav-links {
display: none;
}
.nav-links-mobile {
display: flex;
flex-direction: column;
width: 100%;
}
.mobile-menu-icon {
display: block;
}
}
.active {
border-bottom: 2px solid #fff;
}
Import the CSS file in Navbar.js:
// Navbar.js
import React, { useState } from 'react';
import { NavLink } from 'react-router-dom';
import './Navbar.css'; // Import the CSS file
function Navbar() {
// ...existing code
}
Your Navbar.js should now look like this:
// Navbar.js
import React, { useState } from 'react';
import { NavLink } from 'react-router-dom';
import './Navbar.css';
function Navbar() {
const [isMobile, setIsMobile] = useState(false);
const [isAuthenticated, setIsAuthenticated] = useState(false);
return (
<nav>
<h2>My Website</h2>
<div
className={isMobile ? 'nav-links-mobile' : 'nav-links'}
onClick={() => setIsMobile(false)}
>
<NavLink exact to="/" activeClassName="active">Home</NavLink>
<NavLink to="/about" activeClassName="active">About</NavLink>
<NavLink to="/contact" activeClassName="active">Contact</NavLink>
{isAuthenticated ? (
<NavLink to="/logout" onClick={() => setIsAuthenticated(false)}>Logout</NavLink>
) : (
<NavLink to="/login" onClick={() => setIsAuthenticated(true)}>Login</NavLink>
)}
</div>
<button
className="mobile-menu-icon"
onClick={() => setIsMobile(!isMobile)}
>
{isMobile ? '✖' : '☰'}
</button>
</nav>
);
}
export default Navbar;
You've now created a dynamic React navbar that:
Note: For a production application, consider using more robust state management (like Redux or Context API) and authentication methods.
Additional Tips:
Not yet included in a published release.