React JS Navigation Bar - reactjs

App.js
import NewsSec from "./News/NewsSec";
import ScoreSec from "./ScoreSec/ScoreSec";
import Menu from "./Sidebar/Menu";
import "./styles.css";
import { GiHamburgerMenu } from "react-icons/gi";
import React, { useState } from "react";
export default function App() {
const [showMediaIcons, setShowMediaIcons] = useState(false);
return (
<div className="App">
<div className="head">
<div className="navicon">
<a
href="/"
onClick={() => {
setShowMediaIcons(!showMediaIcons);
}}
>
<GiHamburgerMenu />{" "}
</a>
</div>
<div className="logo">Project</div>
<div className="weather">weather section</div>
</div>
<div className="main">
<div className="nav-section">
<Menu classes={showMediaIcons ? "mobile-view navbar" : "navbar"} />
</div>
<div className="news-section">
<NewsSec />
</div>
<div className="score-section">
<ScoreSec />
</div>
</div>
</div>
);
Menu.js
import React from "react";
import "./Navbar.css";
const Menu = (props) => {
return (
<>
<div className={props.classes}>
<ul>
<li>Home</li>
<li>News</li>
<li>Sports</li>
<li>Weather</li>
<li>About</li>
</ul>
</div>
</>
);
};
export default Menu;
i was trying to make a responsive navigation bar. the navigation bar is actually a sidebar. i used the props to pass the 'className' from App.js to Menu.js because i called the function in App.js
For testing, I tried changing the nav colour to Red. But on clicking Hamburger icon, the colour changes to Red and changed back to normal automatically. Please help folks

It seems that a might be reloading the page with href="/", resetting the state showMediaIcons to its initial value of false.
If the purpose of GiHamburgerMenu is to toggle display for Menu, it might not need to be wrapped in a, instead try add the onClick on the icon or on the wrapping div:
<div className="navicon">
<GiHamburgerMenu
onClick={() => {
setShowMediaIcons((prev) => !prev);
}}
/>
</div>

Related

React error UseContext - Cannot destructure property 'menuActive' of '(0 , react__WEBPACK_IMPORTED_MODULE_0__.useContext)(...)' as it is undefined

I want to create a burger menu
My task is to transfer the **useState **from the "Button" component to the "Navigation" component
**When clicked, it should appear
My code
Main Context component
import React from "react"
export const Context = React.createContext()
Button Component
import React,{useState, useContext} from 'react'
import {Context} from '../../../Context/Context' // I define context
import arrow from '../../../Assets/img/arrow_back.svg'
const Button = () => {
const [menuActive, setMenuActive] = useState(false)
return (
<Context.Provider value={{ // I define context
menuActive,setMenuActive
}}>
<div className='w-11 h-11 border-2 border-[#191619] rounded grid place-items-center'>
<a href="/" onClick={() => setMenuActive(!menuActive)}><img src={arrow} className="w-full flex" alt="" /></a>
</div>
</Context.Provider>
)
}
export default Button
App JS
import React,{useContext} from 'react'
import {Context} from './Context/Context'
import Header from "./Components/Header/Header";
import Navigation from "./Components/Navigation/Navigation";
function App() {
const {menuActive,setMenuActive} = useContext(Context)
return (
<div className="App">
<Header/>
<Navigation active={menuActive} setActive={setMenuActive}/> I define context and i pass the props to the block Navigation
</div>
);
}
export default App;
Navigation
return (
<div className={active ? 'menu active' : 'menu'}> // Just checking and adding a class
<div className="blur" />
<div className="menu__content">
<div className="menu__header"></div>
<ul>
{
items.map(item =>
<li className='flex p-3'>
<span class="material-symbols-outlined text-white">{item.icon}</span>
<a className='text-white pl-3' href={item.href}>{item.value}</a>
<span className='material-icons'></span>
</li>
)
}
</ul>
</div>
</div>
)

i want to build sidebar on my own but the function don't work

Sorry for my bad English speak please help me to build a responsive sidebar menu. when i try to build a sidebar menu responsive I reach this type of error : TypeError: document.getElementByClassName is not a function. I haven't an idea where's the error exactly so this is my code of the sidebar menu:
import React from 'react';
import {useEffect} from 'react';
import SideNav, { Toggle, Nav, NavItem, NavIcon, NavText } from '#trendmicro/react-sidenav';
import '#trendmicro/react-sidenav/dist/react-sidenav.css';
import {listProducts} from '../actions/productActions';
import {useSelector, useDispatch} from 'react-redux';
function w3_open() {
document.getElementByClassName("sidebar").style.width = "100%";
document.getElementByClassName("sidebar").style.display = "block";
}
function w3_close() {
document.getElementByClassName("sidebar").style.display = "none";
}
export default function SideBarMenu() {
const dispatch = useDispatch();
useEffect(()=> {
dispatch(listProducts);
}, [])
const productList = useSelector((state) => state.productList);
const {products} = productList;
console.log(products);
return (
<div className="wrapper" >
<div className="section">
<div className="top_navbar">
<div className="hamburger">
<a href="#">
<i className="fa fa-bars" onClick={w3_open(), w3_close()} ><span className="bar-icon"> All products </span></i>
</a>
</div>
</div>
</div>
<div className="sidebar">
profile image & text
menu item
</div>
</div>
)
}
and this is the app.js file :
import React from 'react';
import {BrowserRouter, Routes, Route} from 'react-router-dom';
import HomePage from './Pages/HomePage';
import ProductPage from './Pages/productPage';
import SideBarMenu from './components/SideBarMenu';
function App() {
return (
<BrowserRouter>
<div className="grid-container" >
<header className="row" >
<div>
<a className="brand" href="/">My shop</a>
</div>
<div>
Cart
Sign In
</div>
</header>
<main>
<SideBarMenu ></SideBarMenu>
<Routes>
<Route path='/product/:id' element={<ProductPage /> } />
<Route path='/' element={<HomePage />} exact />
</Routes>
</main>
<footer className="row center" >All right reserved</footer>
</div>
</BrowserRouter>
);
}
export default App;
document.getElementByClassName(...) doesn't exist in JavaScript. You should use document.getElementsByClassName(...) (notice the plural in Elements).
Replace document.getElementByClassName("sidebar") with document.getElementsByClassName("sidebar")[0].
Update
To fix the error that you have right now, I would suggest to use a variable and useState Hook to display or hide the sidebar.
Example:
const [open, setOpen] = useState(false)
const handleSidebar = () => {
setOpen(!open)
}
return(
<div className = "wrapper" >
<div className = "section">
<div className = "top_navbar">
<div className = "hamburger">
<a href = "#">
<i className="fa fa-bars" onClick = {handleSidebar}><span className = "bar-icon"> All products </span></i>
</a>
</div>
</div>
</div>
{ open
? <div className = "sidebar">
profile image & text menu item
</div>
: null
}
</div>
)

Passing Data from Parent to Child In React

I am testing if I can pass data from the parent App.js to a child component named Contact. I want to pass data when clicking on a button.
Here is the App.js file:
import React, { useState } from 'react';
import { Button } from 'semantic-ui-react';
import './App.css';
import Header from './Components/Header';
import Tabs from './Components/Tabs';
import Footer from './Components/Footer';
import SimpleMap from './Components/SimpleMap';
import NewPatient from './Components/NewPatient';
import Contact from './Components/Contact';
function App() {
const [data,setData] = useState(false);
const parentToChild = () => {
setData("This is information from parent to child.");
}
return (
<div className="App">
<Header />
<Contact parentToChild={data} />
<Tabs>
<div label="Home" class="home-page">
<img src="https://news.usc.edu/files/2020/06/covid_vaccine_stock.jpg"></img>
</div>
<div label="New Patient">
<NewPatient />
</div>
<div label="Locations">
<SimpleMap />
</div>
<div label="Appointments">
</div>
</Tabs>
<Footer />
</div>
);
}
export default App;
Here is the child component I want the data to be displayed from the parent data when I click on the button:
import React from 'react';
import './footer.css'
function Footer({parentToChild})
{
return (
<div className="Footer">
<div className="container">
<div className="row">
<div className="col">
<h4 className="block ">About Us</h4>
</div>
<div className="col">
<h4 onClick={() => parentToChild(true)} className="block" >Contact</h4>
</div>
<div className="col">
<h4><a className="block" href={"https://www.cdc.gov/coronavirus/2019-ncov/faq.html"}>COVID-19 FAQ</a></h4>
</div>
<div className="col">
<h4 className="block"><a className="block" href={"https://www.cdc.gov/coronavirus/2019-ncov/if-you-are-sick/quarantine.html"}>CDC Guidelines</a></h4>
</div>
</div>
</div>
</div>
);
}
export default Footer;
I am testing this functionality because I want to eventually display a popup form when I click on a button. I want to use the data from the parent to trigger a popup in a child component. The following code says that parentToChild is not a function. Why do I get this error?
You need to pass your parentToChild function to your Footer Component:
<Footer parentToChild={parentToChild}/>
And if you want to show data from your child, you will need to declare it in your params:
const parentToChild = (input) => {
console.log('Info from child' ,input);
setData("This is information from parent to child.");
}
You must pass that information via props to the Footer component,
<Footer parentToChild={parentToChild} />
This will trigger the callback.
you can use state.
const [state, setState] = useState('')
App.js
const buttonThatYouWantTobeClicked = (dataShouldGotoComponent) => {
setState(dataShouldGotoComponent)
}
<Footer data={state}/>
then in footer.js you can check if value of props is not undefiend or null, so do something

How to make horizontal scrollbar in React JS such as the one Google has?

what is the easiest way of asking this in react js?
How can I make a scroll bar using this code.see image expected output?
*[https://i.stack.imgur.com/nex4t.png][1]
import React from 'react';
import ScrollContainer from "react-indiana-drag-scroll";
import './App.css';
const list = new Array(15).fill(1).map((_, index) => index + 1);
const Arrow = ({ text, className }) => {
return <div className={className}>{text}</div>;
};
class Nav extends React.Component{
render(){
return(
<div className="Head">
<div className="Filters">
Filters
</div>
<div className="categories">
<ScrollContainer className="pn-ProductNav">
<div className="pn-ProductNav_Contents ">
{list.map(el => (
<a href="googl.com" className="pn-ProductNav_Link" >
<div className="inner" key={el}>
Chairs
</div>
</a>
))}
</div>
</ScrollContainer>
</div>
</div>
);
}
}
export default Nav;

How to get another component when I click h1 tag in react using hooks?

I am working on react project in that project Home.js is Parent component and Test.js is Child component. In Home.js component I have tag here what I am trying to do is If I Click tag I need to call Test.js component. For this In Home.js I created one function and, To that function I passed Test.js component. and I passed that function via onClick method to h1 tag but its not working. How to achieve this.
This is App.js
import React from 'react';
import './App.css';
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";
import Navbar from './Components/Navbar/Navbar';
import Home from "./Pages/Home/Home";
import Test from "./Pages/Test/Test";
function App() {
return (
<div className="App">
<Router>
<Navbar></Navbar>
<Switch>
<Route exact path='/'><Home></Home></Route>
<Route path='/test'><Test></Test></Route>
</Switch>
</Router>
</div>
);
}
export default App;
This is Navbar.js
import React from 'react';
import './Navbar.css';
import { Link } from 'react-router-dom';
export default function Navbar() {
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<nav className="navbar navbar-expand-lg navbar-light bg-light">
<a className="navbar-brand" href="#">Navbar</a>
<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarNav">
<ul className="navbar-nav">
<li className="nav-item active">
<Link className='nav-link' to='/'>Home</Link>
</li>
</ul>
</div>
</nav>
</div>
</div>
</div>
)
}
This is Home.js
import React from 'react';
import './Home.css';
import { Link } from 'react-router-dom';
import Test from '../Test/Test';
export default function Home() {
const Test = () => {
<Test></Test>
}
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<div>
<h1 onClick={Test}>Cruse</h1>
</div>
</div>
</div>
</div>
)
}
This is Test.js
import React from 'react'
export default function Test() {
return (
<div>
<h1>Test works</h1>
</div>
)
}
I would say you are using event handlers wrong. Their purpose is to produce some kind of side effects, and they don't return a direct result. If you wish to show new element on click i recommend using react state and something like this:
export default function Home() {
const [isTestVisible, setTestVisible] = useState(false);
const showTest= () => setTestVisible(true);
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<div>
<h1 onClick={showTest}>Cruse</h1>
{isTestVisible && <Test />}
</div>
</div>
</div>
</div>
)
}
In your state you could save different props for each test component so you can customize them. Hope this helps :)
Using states is the correct answer as #Kaca992 said. But for what you do, you can do something like this:
const Test = () => {
ReactDOM.render(<Test/>, document.getElementsByTagName("h1")[0]);
}

Resources