Responsive Navbar-Toggle button not working - reactjs

I am trying to do responsive navbar by using bootstrap in reactjs.I have update the show in state so as i do like show or hide the element but the toggle button is not trigger.If i use the script link in bootstrap its working but i don't want to use the scripting links and want to update manual.so help me out.
Here is my code:
import React, { Component } from "react";
export default class NavBar extends Component {
constructor(props) {
super(props);
this.state = {
show: true,
};
}
showToggle = () => {
this.setState({
show: false,
});
};
render() {
return (
<div className="container-fluid">
<div className="row">
<div className="col-lg-12 col-md-12 col-sm-12">
<nav className="navbar navbar-expand-lg navbar-dark bg-dark">
<div className="navbar-brand">
<img
alt="logo"
src="./images/or.png"
width="100px"
height="30"
className="d-inline-block align-top img-fluid"
/>
{/* <span className="nav-brand">Ortho Life Hospital</span> */}
</div>
<button
className="navbar-toggler"
type="button"
// data-toggle="collapse"
// data-target="#navbarSupportedContent"
// aria-controls="navbarSupportedContent"
// aria-expanded="false"
// aria-label="Toggle navigation"
onClick={this.showToggle}
>
<span className="navbar-toggler-icon"></span>
</button>
{this.state.show ? (
<div
className="collapse navbar-collapse"
id="navbarSupportedContent"
>
<ul className="navbar-nav ml-auto">
<li className="nav-item ml-5">
<a className="nav-link" href="/">
Home
</a>
</li>
<li className="nav-item ml-5">
<div className="dropdown">
<a
className="nav-link"
href="/about"
id="dropdownMenuButton"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"
>
About Us
</a>
<div
className="dropdown-menu"
aria-labelledby="dropdownMenuButton"
>
<a className="dropdown-item" href="/about/#history">
History
</a>
<a className="dropdown-item" href="/about/#vision">
Vision and Mission
</a>
<a className="dropdown-item" href="/about/#whyortho">
Why Ortho?
</a>
</div>
</div>
</li>
<li className="nav-item ml-5">
<div className="dropdown">
<a
className="nav-link"
href="/department"
id="dropDownDepartment"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"
>
Department
</a>
<div
className="dropdown-menu"
aria-labelledby="dropDownDepartment"
>
<a
className="dropdown-item"
href="/department/#medical"
>
Medical
</a>
<a
className="dropdown-item"
href="/department/#allied"
>
Allied
</a>
</div>
</div>
</li>
<li className="nav-item ml-5">
<a className="nav-link" href="/media">
Media
</a>
</li>
<li className="nav-item ml-5">
<a className="nav-link" href="/contactus">
Contact Us
</a>
</li>
</ul>
</div>
) : null}
</nav>
</div>
</div>
</div>
);
}
}

Related

Change Bootstrap class depending of component state in React

I have a react component called NavBar. It gets a variable from another component (App.js) which specifies if the page is in dark mode or not. The variable is called "pageMode". I want to change the class of the navBar from navbar-light to navbar-dark depending of the value of the "pageMode" var.
HereĀ“s the code
import React from 'react';
class NavBar extends React.Component{
render(){
if(this.props.pageMode === "dark"){
//Here I want to do something to render the component using navbar-dark class
}
else{
//Render using navbar-light class
}
return <nav className="navbar navbar-expand-lg [navbar-light] bg-light">
<a className="navbar-brand NavBar-title-fix" href="#">Inicio</a>
<button className="navbar-toggler" type="button" data-toggle="collapse" data-
target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-
expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav mr-auto">
<li className="nav-item active">
<a className="nav-link" href="#">Proyectos</a>
</li>
<li className="nav-item">
<a className="nav-link" href="#">Sobre mi</a>
</li>
</ul>
</div>
</nav>
}
}
export default NavBar;
In the render return state the class between [] is the one that i want to be changed depending of the variable value.
Thanks for reading!
import React from 'react';
class NavBar extends React.Component{
render(){
const themeClass = {
dark: '[navbar-dark]',
light: '[navbar-light]'
}[this.props.pageMode]
return <nav className={`navbar navbar-expand-lg ${themeClass} bg-light`}>
<a className="navbar-brand NavBar-title-fix" href="#">Inicio</a>
<button className="navbar-toggler" type="button" data-toggle="collapse" data-
target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-
expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav mr-auto">
<li className="nav-item active">
<a className="nav-link" href="#">Proyectos</a>
</li>
<li className="nav-item">
<a className="nav-link" href="#">Sobre mi</a>
</li>
</ul>
</div>
</nav>
}
}

How to change background-color of navigation when redirecting from homepage to other pages using useLocation on Scroll in React.js?

BELOW IS MY CODE : HEADER.JS
When I click on shop button in navigation on homepage -> http://localhost:3000/ then it should go to -> http://localhost:3000/product-listing and should add a bg-black class on nav tag and should stay black background.
IMPORTS---
import React, { useState, useEffect } from "react";
import Loading from "../other/Loading";
import PageNotFound from "../other/PageNotFound";
import { NavLink, useLocation } from "react-router-dom";
const Header = () => {
const [loading, setLoading] = useState(true);
const [headerData, setHeaderData] = useState(null);
const [bgBlack, setBgBlack] = useState("");
const [border, setBorder] = useState("");
const [textColor, setTextColor] = useState("");
const location = useLocation();
console.log(location.pathname);
USE EFFECT---
useEffect(() => {
getHeaderData();
window.addEventListener("scroll", changeBackground);
return () => {
window.removeEventListener("scroll", changeBackground);
};
}, []);
GET HEADER NAVIGATION DATA---
const getHeaderData = async () => {
try {
const response = await fetch("http://localhost:8000/navLinks");
const data = await response.json();
if (data) {
setLoading(false);
setHeaderData(data);
} else {
setHeaderData("DATA NOT FOUND");
}
} catch (err) {
if (err) {
console.log("OOPS... SOMETHING WENT WRONG");
}
}
};
CHANGE NAV BACKGROUND-COLOR---
const changeBackground = () => {
if (window.scrollY > 5) {
setBgBlack("bg-black");
setBorder("border-bottom");
setTextColor("text-white");
} else {
setBgBlack("");
setBorder("");
setTextColor("");
}
};
IF LOADING IS TRUE LOAD LOADING COMPONENT---
if (loading) {
return (
<>
<Loading loadingHeader="Loading Header" />
</>
);
}
// console.log(headerData);
RETURN JSX---
return (
<>
{headerData ? (
<header>
<nav
className={`navbar navbar-expand-lg navbar-light ${bgBlack} fixed-top`}
>
<div className="container">
<div>
<button
className="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span className="text-white">
<i className="fa fa-bars"></i>
</span>
</button>
</div>
<div className="order-lg-0">
<NavLink className="navbar-brand" to="/">
<img
width="50"
src={headerData.logo.url}
className="img-fluid"
alt=""
/>
</NavLink>
</div>
<div className="order-lg-2 d-lg-none">
<span
className="text-white d-flex"
data-toggle="modal"
data-target="#mobileCartModal"
>
<span className="d-inline-block mr-1">
<i className="fa fa-shopping-cart"></i>
</span>
<span className="d-inline-block">0</span>
</span>
</div>
<div
className="collapse navbar-collapse order-lg-1"
id="navbarSupportedContent"
>
<ul className="navbar-nav mr-auto px-3">
{/* <li className="nav-item dropdown">
<a
className="nav-link dropdown-toggle"
href="#"
id="navbarDropdown"
role="button"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"
>
Packages
</a>
<div
className="dropdown-menu"
aria-labelledby="navbarDropdown"
>
<a className="dropdown-item" href="product.html">
Subscriptions
</a>
<a className="dropdown-item" href="product.html">
Value Packs
</a>
</div>
</li>
<li className="nav-item dropdown">
<a
className="nav-link dropdown-toggle"
href="#"
id="navbarDropdown"
role="button"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"
>
Juices
</a>
<div
className="dropdown-menu"
aria-labelledby="navbarDropdown"
>
<a className="dropdown-item" href="product.html">
Juices
</a>
<a className="dropdown-item" href="product.html">
Almond Milks
</a>
<a className="dropdown-item" href="product.html">
Protien Milkshake
</a>
<a className="dropdown-item" href="product.html">
Cleanses
</a>
</div>
</li> */}
<li className="nav-item">
<NavLink
className="nav-link"
to="/product-listing"
data-toggle="modal"
data-target="#loginModal"
>
Shop
</NavLink>
</li>
<li className="nav-item dropdown">
<a
className="nav-link dropdown-toggle"
href="#"
id="navbarDropdown"
role="button"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"
>
Learn
</a>
<div
className="dropdown-menu"
aria-labelledby="navbarDropdown"
>
<a className="dropdown-item" href="#">
Process
</a>
<a className="dropdown-item" href="#">
About Us
</a>
<a className="dropdown-item" href="#">
Blog
</a>
<a className="dropdown-item" href="#">
News
</a>
<a className="dropdown-item" href="#">
Beyond The Bottle
</a>
</div>
</li>
<li className="nav-item">
<a
className="nav-link"
href="#"
data-toggle="modal"
data-target="#loginModal"
>
Login
</a>
</li>
<li className="nav-item">
<a
className="nav-link"
href="#"
data-toggle="modal"
data-target="#signupModal"
>
Sign Up
</a>
</li>
</ul>
<form className="dvSearch my-2 my-lg-0 px-3 position-relative">
<input
className={`form-control mr-sm-2 ${border}`}
type="text"
placeholder="Search"
aria-label="Search"
/>
<button
className="btn my-2 my-sm-0 position-absolute"
type="submit"
>
<i className={`fa fa-search ${textColor}`}></i>
</button>
</form>
</div>
</div>
</nav>
</header>
) : (
<PageNotFound />
)}
</>
);
};
EXPORT DEFAULT HEADER.JS---
export default Header;

Trying to create a sidebar with dropdown using the react-bootstrap package

This is the HTML code I'm trying to convert using the react-bootstrap package
i couldn't find any details for the sidebar like this.
<nav class="sb-sidenav accordion sb-sidenav-light" id="sidenavAccordion">
<div class="sb-sidenav-menu">
<div class="nav">
<div class="sb-sidenav-menu-heading">Core</div>
<a class="nav-link" href="index.html">
<div class="sb-nav-link-icon"><i class="fas fa-tachometer-alt"></i></div>
Dashboard
</a>
<div class="sb-sidenav-menu-heading">Interface</div>
<a class="nav-link collapsed" href="#" data-bs-toggle="collapse" data-bs-target="#collapseLayouts" aria-expanded="false" aria-controls="collapseLayouts">
<div class="sb-nav-link-icon"><i class="fas fa-columns"></i></div>
Layouts
<div class="sb-sidenav-collapse-arrow"><i class="fas fa-angle-down"></i></div>
</a>
<div class="collapse" id="collapseLayouts" aria-labelledby="headingOne" data-bs-parent="#sidenavAccordion">
<nav class="sb-sidenav-menu-nested nav">
<a class="nav-link" href="layout-static.html">Static Navigation</a>
<a class="nav-link" href="layout-sidenav-light.html">Light Sidenav</a>
</nav>
</div>
</div>
</div>
</nav>
i have partially converted it but the submenu is not working as in the image
<Navbar bg="" variant="" expand="" bsPrefix="accordion" className="sb-sidenav sb-sidenav-dark">
<div className="sb-sidenav-menu">
<Nav className="nav" activeKey="/home">
<div className="sidebar-sticky"></div>
<Nav.Item>
<Nav.Link href="/home">Active</Nav.Link>
</Nav.Item>
<Nav.Item>
<Nav.Link eventKey="link-1">Link</Nav.Link>
</Nav.Item>
<Nav.Item>
<Nav.Link eventKey="link-2">Link</Nav.Link>
</Nav.Item>
</Nav>
</div>
</Navbar>
use this:
<nav class="sb-sidenav accordion sb-sidenav-light" id="sidenavAccordion">
<div class="sb-sidenav-menu">
<div class="nav">
<div class="sb-sidenav-menu-heading">Core</div>
<a class="nav-link" href="index.html">
<div class="sb-nav-link-icon"><i class="fas fa-tachometer-alt"></i></div>
Dashboard
</a>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria- expanded="false">
Layouts
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Static Navigation</a>
<a class="dropdown-item" href="#">Light Sidenav</a>
</li>
</div>
</div>
</nav>
check this:https://jsfiddle.net/69x8pzmy/2/

how to close the menu when clicked

import React from 'react'
import {Link} from "react-router-dom"
const Navbar = () => {
return (
<div className="navbar-dark bg-dark shadow sticky-top">
<div className="container">
<nav className="navbar navbar-expand-lg">
<div className="container-fluid">
<Link to="/" className="navbar-brand"><span className="effect">I <span className="rotation">Design</span> <span className="flash">Views</span></span>
</Link>
<button className="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav m-auto p-3 mb-2 mb-lg-0">
<li className="nav-item">
<Link to="/" className="nav-link active">Inicio</Link>
</li>
<li className="nav-item">
<Link to="/about" className="nav-link active">Bio</Link>
</li>
<li className="nav-item">
<Link to="/servicios" className="nav-link active">Servicios</Link>
</li>
<li className="nav-item">
<Link to="/contacto" className="nav-link active">Contacto</Link>
</li>
</ul>
</div>
</div>
</nav>
</div>
</div>
)
}
export default Navbar
The previous versions of the bootstrap menu came by default to close the menu when clicking, now that I update I don't really know how to do it with react js, if anyone can help me I would appreciate it
You might have missed out adding the bootstrap's js resource. Try adding the below in your index.html.
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
I have used your code as it is, by just adding the bootstrap js resource it's working as expected. Below is the example,
const Navbar = () => {
return (
<div className="navbar-dark bg-dark shadow sticky-top">
<div className="container">
<nav className="navbar navbar-expand-lg">
<div className="container-fluid">
<a href="/" className="navbar-brand"><span className="effect">I <span className="rotation">Design</span> <span className="flash">Views</span></span>
</a>
<button className="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav m-auto p-3 mb-2 mb-lg-0">
<li className="nav-item">
Inicio
</li>
<li className="nav-item">
Bio
</li>
<li className="nav-item">
Servicios
</li>
<li className="nav-item">
Contacto
</li>
</ul>
</div>
</div>
</nav>
</div>
</div>
)
}
ReactDOM.render(<Navbar />, document.getElementById("react"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
<div id="react"></div>
Note: Just for the demo purpose I have replaced Links with anchors.

How to Close the menu after click on menu item

I have a menu on website. Below is the HTML code.
<header class="navbar navbar-sticky navbar-expand-lg navbar-dark">
<div class="container position-relative">
<a class="navbar-brand" href="./index.html">
<img class="navbar-brand-regular" src="logo.png"/>
<img class="navbar-brand-sticky" src="logo.png"/>
</a>
<button class="navbar-toggler d-lg-none" type="button" data-toggle="navbarToggler" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-inner">
<!-- Mobile Menu Toggler -->
<button class="navbar-toggler d-lg-none" type="button" data-toggle="navbarToggler" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<nav>
<ul class="navbar-nav" id="navbar-nav">
<li class="nav-item">
<a class="nav-link scroll" href="#home">Home</a>
</li>
<li class="nav-item">
<a class="nav-link scroll" href="#about">About</a>
</li>
</ul>
</nav>
</div>
</div>
</header>
On Mobile menu, when i click on a menu item, the menu did not closed. Below is the script code.
Did I miss anything?
// MAIN MENU TOGGLER ICON (MOBILE SITE ONLY)
$('[data-toggle="navbarToggler"]').click(function () {
$('.navbar').toggleClass('active');
$('body').toggleClass('canvas-open');
});
// MAIN MENU TOGGLER ICON
$('.navbar-toggler').click(function () {
$('.navbar-toggler-icon').toggleClass('active');
});
Bootstrap will handle all toggle/collapse by itself you do not need to write extra JS. Toggle button must not duplicate as you did to avoid messing up the navbar functioning.
Below is the answer. I optimized your params in few places. If my answer works for you, please tick it as final answer and upvote it so other people with same problem will get help too. Cheers
<nav class="navbar navbar-dark navbar-expand-sm fixed-top">
<div class="container">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#Navbar">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand mr-auto" href="./index.html"> <img src="#" height="#" width="#"> </a>
<div class="collapse navbar-collapse" id="Navbar">
<ul class="navbar-nav mr-auto">
<li class="navbar-item active"><a class="nav-link" href="./index.html"> Home</a></li>
<li class="navbar-item"><a class="nav-link" href="./aboutus.html">About</a></li>
</ul>
</div>
</div>
</nav>

Resources