Bootstrap navbar toggler is not working in reactjs - reactjs

My version of Bootstrap is 5.2.
navbar.jsx
import React, { Component } from "react";
import { NavLink, Link } from "react-router-dom";
class Navbar extends Component {
state = {};
render() {
return (
<nav className="navbar navbar-dark mb-2 navbar-expand-lg bg-dark">
<div className="container">
<Link className="navbar-brand" to="#">
Vidly
</Link>
<button
className="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarNavAltMarkup"
aria-controls="navbarNavAltMarkup"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span className="navbar-toggler-icon" />
</button>
<div className="collapse navbar-collapse" id="navbarNav">
<ul className="navbar-nav ms-auto">
<li className="nav-item">
<NavLink className="nav-link" aria-current="page" to="/movies">
Movies
</NavLink>
</li>
<li className="nav-item">
<NavLink className="nav-link" to="/rentals">
Rentals
</NavLink>
</li>
<li className="nav-item">
<NavLink className="nav-link" to="/customers">
Customers
</NavLink>
</li>
<li className="nav-item">
<NavLink className="nav-link" to="/login">
Login
</NavLink>
</li>
</ul>
</div>
</div>
</nav>
);
}
}
export default Navbar;
Above is my code here on top navbar-toggle is not working.
I tried more things and read more documents about it but I'm unable to find a solution - kindly need your help.
Thanks in advance <3

I think "bootstrap" and "react-bootstrap" are different.
you can refer to this link
https://react-bootstrap.github.io

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>
}
}

Replacing <a> with <Link> breaks my NavBar

I have this bootstrap NavBar that works perfectly with "a", but when I try to replace the "a" with "Link" from React it pretty much breaks the app, the Navbar stops showing. Is it because of the Bootstrap? I saw a video from youtube that this works, maybe with newer versions this doesn't work?
import React from "react";
import { Link } from "react-router-dom";
<nav className="navbar navbar-expand-lg navbar-light bg-light">
<div className="container-fluid">
<div>
<img width="100" src="/assets/images/logo.jpg"></img>
</div>
<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 me-auto mb-2 mb-lg-0">
<li className="nav-item">
<Link className="nav-link" to="/">Home</Link> <----------------here for ex.
</li>
<li className="nav-item">
<a className="nav-link active" aria-current="page" href="#">
Productos
</a>
</li>
<li className="nav-item">
<a className="nav-link active" aria-current="page" href="#">
Nosotros
</a>
</li>
<li className="nav-item">
<a className="nav-link active" aria-current="page" href="#">
Contacto
</a>
</li>
</ul>
</div>
</div>
</nav>
Judging from the href attribute in your <a> components, I believe you are trying to access a component on the same page using hashes.
Link from react-router-dom does not actually support this feature from what I know. Instead, I suggest installing react-router-hash-link which supports it.
The import can look like
import { HashLink as Link } from 'react-router-hash-link';
And you can have the same component attributes
<li className="nav-item">
<Link className="nav-link active" aria-current="page" to="/#productos">
Productos
</Link>
</li>
You need to enclose your <NavLink/>, <Link/>, <Routes/> and <Route/> components with a Router like <BrowserRouter/>.
So in App.js or another top level component, you need to do it like this:
const App = (props) => {
<BrowserRouter>
<Your sidebar component including NavLink elements/>
<Your app module including Route elements/>
</BrowserRouter>
}

Responsive Navbar-Toggle button not working

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>
);
}
}

How to make selected item as active in sidebar in react js

I have a side bar. It contains Menus. Each Menu has sub menus.
Home
Menu1 ->1.submenu1
->submenu2
Menu2 -> sub1
-> sub2
On selecting the Menu, it should be in active(color:blue). Under that menu1, If we select submenu1 then its color should also be changed. On navigating to submenu1, Sidebar's submenu should not be closed. It should be opened. I have tried with different solutions, but couldn't fix. Please help me to resolve this issue. Thanks in advance..
<aside className="main-sidebar sidebar-dark-primary elevation-4">
<div className="sidebar">
<nav>
<ul className="nav nav-pills nav-sidebar nav-child-indent flex-column" data-widget="treeview" role="menu" data-accordion="false">
<li className="nav-item">
<Link to="/home" className="nav-link active">
<i className="nav-icon fa fa-tachometer"></i>
<p>
Home
</p>
</Link>
</li>
<li className="nav-item has-treeview ">
<Link to="#" className="nav-link">
<i className="nav-icon fa fa-copy"></i>
<p>Menu1 <i className="fa fa-angle-left right"></i>
</p>
</Link>
<ul className="nav nav-treeview">
<li className="nav-item">
<Link to="/submenu1" className="nav-link">
<i className="fa fa-circle nav-icon"></i>
<p>Submenu1</p>
</a>
</li>
<li className="nav-item">
<Link to="/submenu2" className="nav-link">
<i className="fa fa-circle nav-icon"></i>
<p>Submenu2</p>
</a>
</li>
</ul>
</li>
<li className="nav-item has-treeview">
<Link to="#" className="nav-link">
<i className="nav-icon fa fa-book"></i>
<p>Menu2 <i className="fa fa-angle-left right"></i>
</p>
</Link>
<ul className="nav nav-treeview">
<li className="nav-item">
<Link to="/sub1" className="nav-link">
<i className="fa fa-circle nav-icon"></i>
<p>Sub1</p>
</Link>
</li>
<li className="nav-item">
<Link to="/sub2" className="nav-link">
<i className="fa fa-circle nav-icon"></i>
<p>Sub2</p>
</Link>
</li>
</ul>
</li>
</ul>
</nav>
</div>
</aside>
In React js, there are two components to navigate to a route:
<NavLink> and <Link>.
<Link> provides accessible navigation to your react application.
<NavLink> provides styling attributes to the rendered element if matches to the URL alongwith navigation. Some of the styling attributes are activeClassName:String and activeStyle:Object.
Example of activeClassName attribute:
<NavLink to="/" exact activeClassName="selected">Home</NavLink>
<NavLink to="/about" activeClassName="selected">About</NavLink>
And in CSS,
.selected {
fontWeight: "bold",
color: "red"
}
Example of activeStyle:
<NavLink to="/" exact activeStyles={{fontWeight: "bold", color: "red"}}>
Home
</NavLink>

How to render the page based on selected option on the middle part of App, Top and side bar should be static

Sorry if the title is kinda confusing.
I'm making an index page with a side and top navbar with the menu using react router, I have successfully loaded the content up but I want it to load in the main body, the middle of the page.
I've tried separating method like how I used JSP before but its not very effective so to speak
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import Login from './Login';
import * as serviceWorker from './serviceWorker';
import Dropdown from 'react-bootstrap/Dropdown';
import { Link } from 'react-router-dom';
import { BrowserRouter, Route } from "react-router-dom";
import './index.css';
import AssetView from './AssetView';
import DepartmentView from './DepartmentView';
import LocationView from './LocationView';
import EmployeeView from './EmployeeView';
import AddDepartment from './AddDepartment';
import AddLocation from './AddLocation';
const routing = (
<BrowserRouter>
<div>
<nav class="navbar navbar-dark fixed-top bg-dark flex-md-nowrap p-0">
<a class="navbar-brand col-sm-3 col-md-2 mr-0" href="#">Company name</a>
<ul class="navbar-nav px-3">
<li class="nav-item dropdown">
<Dropdown>
<Dropdown.Toggle variant="success" id="dropdown-basic">
Account
</Dropdown.Toggle>
<Dropdown.Menu>
<Dropdown.Item href="#/action-1">Informations</Dropdown.Item>
<Dropdown.Item href="#/action-2">Settings</Dropdown.Item>
<Dropdown.Item href="#/action-3">Signout</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
</li>
</ul>
</nav>
<div class="container-fluid">
<div class="row">
<nav class="col-md-2 d-none d-md-block bg-light sidebar">
<div class="sidebar-sticky">
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-Link active" href="#">
<span data-feather="home"></span>
Dashboard <span className="sr-only">(current)</span>
</a>
</li>
<h6 className="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
<span>Assets Management</span>
<a className="d-flex align-items-center text-muted" href="#">
<span data-feather="plus-circle" />
</a>
</h6>
<li className="nav-item">
<Link to="/AssetView">
<a class="nav-link" href="#">
<span data-feather="file"></span>
View All Assets
</a>
</Link>
</li>
</ul>
<h6 className="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
<span>Company Management</span>
<a className="d-flex align-items-center text-muted" href="#">
<span data-feather="plus-circle" />
</a>
</h6>
<ul className="nav flex-column mb-2">
<li className="nav-item">
<Link to="/DepartmentView">
<a class="nav-link" href="#">
<span data-feather="file"></span>
View All Departments
</a>
</Link>
</li>
<li className="nav-item">
<Link to="/AddDepartment">
<a class="nav-link" href="#">
<span data-feather="file"></span>
Add New Department
</a>
</Link>
</li>
<li className="nav-item">
<Link to="/LocationView">
<a class="nav-link" href="#">
<span data-feather="file"></span>
View All Locations
</a>
</Link>
</li>
<li className="nav-item">
<Link to="/AddLocation">
<a class="nav-link" href="#">
<span data-feather="file"></span>
Add New Location
</a>
</Link>
</li>
</ul>
<h6 className="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
<span>Employees Management</span>
<a className="d-flex align-items-center text-muted" href="#">
<span data-feather="plus-circle" />
</a>
</h6>
<ul className="nav flex-column mb-2">
<li className="nav-item">
<Link to="/EmployeeView">
<a class="nav-link" href="#">
<span data-feather="file"></span>
View All Employees
</a>
</Link>
</li>
</ul>
</div>
</nav>
</div>
</div>
<Route exact path="/AssetView" component={AssetView} />
<Route path="/DepartmentView" component={DepartmentView} />
<Route path="/LocationView" component={LocationView} />
<Route path="/EmployeeView" component={EmployeeView} />
<Route path="/AddDepartment" component={AddDepartment} />
<Route path="/AddLocation" component={AddLocation} />
</div>
</BrowserRouter>
)
ReactDOM.render(routing, document.getElementById('root'));
serviceWorker.unregister();
This is how the page looks right now, I really wish to make the content of the page appear in the middle
https://photos.app.goo.gl/BSuZuvGhiQQetmit5
You mean, you have to create a dashboard-like app
Where top and sidebar is static and on click of the sidebar the middle part change
Then you can check this repo
https://github.com/designrevision/shards-dashboard-react
Here is the master page https://github.com/DesignRevision/shards-dashboard-react/blob/master/src/layouts/Default.js
import React from "react";
import PropTypes from "prop-types";
import { Container, Row, Col } from "shards-react";
import MainNavbar from "../components/layout/MainNavbar/MainNavbar";
import MainSidebar from "../components/layout/MainSidebar/MainSidebar";
import MainFooter from "../components/layout/MainFooter";
const DefaultLayout = ({ children, noNavbar, noFooter }) => (
<Container fluid>
<Row>
<MainSidebar />
<Col
className="main-content p-0"
lg={{ size: 10, offset: 2 }}
md={{ size: 9, offset: 3 }}
sm="12"
tag="main"
>
{!noNavbar && <MainNavbar />}
{children}
{!noFooter && <MainFooter />}
</Col>
</Row>
</Container>
);
DefaultLayout.propTypes = {
/**
* Whether to display the navbar, or not.
*/
noNavbar: PropTypes.bool,
/**
* Whether to display the footer, or not.
*/
noFooter: PropTypes.bool
};
DefaultLayout.defaultProps = {
noNavbar: false,
noFooter: false
};
export default DefaultLayout;
The correct answer has been replied by #ravibagul91, thank you.
I removed the class="sidebar-sticky" of the sidebar and add the .sidebar{position:absolute;top:0;bottom:0} in my index.css and it works just fine as the image I just uploaded
https://photos.app.goo.gl/m744U7R4mACAUXq37
Thank you #piyush-dhamecha for the recommendation, it is somewhat similar to what I need, but truth be told, it's still kinda advance for me, but I appreciate it and will keep it for future reference.

Resources