I am following a beginner tutorial for an ecommerce website and while making the Sign in popup, I made a component for it and mounted it on the navbar component. It will open the popup when you click on the sign in button.
import React, { useState } from 'react'
import './Navbar.css'
import { assets } from '../../assets/assets'
import { Link } from 'react-router-dom';
const Navbar = ({setShowLogin}) => {
const [menu, setMenu] = useState("home");
return (
<div className='navbar'>
<img src={assets.logo} alt="" className='logo' />
<ul className="navbar-menu">
<Link to='/' onClick={()=>setMenu("home")} className {menu==="home"?"active":""}>home</Link>
<a href='#explore-menu' onClick={()=>setMenu("menu")} className={menu==="menu"?"active":""}>menu</a>
<a href='#app-download' onClick={()=>setMenu("mobile-app")} className={menu==="mobile-app"?"active":""}>mobile-app</a>
<a href='#footer' onClick={()=>setMenu("contact-us")} className={menu==="contact-us"?"active":""}>contact us</a>
<div className="navbar-right">
<img src={assets.search_icon} alt="" />
<div className="navbar-search-icon">
<img src={assets.basket_icon} alt="" />
<div className="dot"></div>
</div>
<button onClick={()=>setShowLogin(true)}>sign in</button>
</div>
</ul>
</div>
)
}
export default Navbar
Here I am using the useState hook to make the showLogin and setShowLogin functionalities inside App.jsx and initializing it as false. I then pass the props to the Navbar.
import React, { useState } from "react";
import Navbar from "./components/Navbar/Navbar";
import { Route, Routes } from "react-router-dom";
import Home from "./pages/Home/Home";
import Cart from "./pages/Cart/Cart";
import PlaceOrder from "./pages/PlaceOrder/PlaceOrder";
import Footer from "./components/Footer/Footer";
import LoginPopup from "./components/LoginPopup/LoginPopup";
const App = () => {
const [showLogin, setShowLogin] = useState(false);
return (
<>
{showLogin?<LoginPopup setShowLogin={setShowLogin} />:<></>}
<div className="app">
<Navbar setShowLogin={setShowLogin} />
<Routes>
<Route path="/" element={<Home />} />
<Route path="/cart" element={<Cart />} />
<Route path="/order" element={<PlaceOrder />} />
</Routes>
</div>
<Footer />
</>
);
};
export default App;
Then, I added an image inside the popup component for the purpose of closing the popup. But even when I set it to false it won't close.
import React, { useState } from 'react'
import './LoginPopup.css'
import { assets } from '../../assets/assets';
const LoginPopup = (setShowLogin) => {
const [currState, setCurrState] = useState("Sign Up");
return (
<div className='login-popup'>
<form className="login-popup-container">
<div className="login-popup-title">
<h2>{currState}</h2>
<img onClick={()=>setShowLogin(false)} src={assets.cross_icon} alt="" />
</div>
</form>
</div>
)
}
export default LoginPopup
Haven't really completed all of it, but in the tutorial the guy is able to close it right after coding it and I double checked to see if I missed something. What am I doing wrong?
You are not receiving the prop correctly, you need to destructure setShowLogin
like this:
const LoginPopup = ({setShowLogin}) => {
const [currState, setCurrState] = useState("Sign Up");
return (
<div className='login-popup'>
<form className="login-popup-container">
<div className="login-popup-title">
<h2>{currState}</h2>
<img onClick={()=>setShowLogin(false)} src={assets.cross_icon} alt="" />
</div>
</form>
</div>
)
}
Or like this:
const LoginPopup = (props) => {
const [currState, setCurrState] = useState("Sign Up");
const setShowLogin = props.setShowLogin;
return (
<div className='login-popup'>
<form className="login-popup-container">
<div className="login-popup-title">
<h2>{currState}</h2>
<img onClick={()=>setShowLogin(false)} src={assets.cross_icon} alt="" />
</div>
</form>
</div>
)
}