2021-12-11

Tech Programing

程式人小天地

How can I close my hamburger menu when clicking on a link using React JS? (using ReactJS hooks)

2 min read


I want to close my hamburger menu using ReactJS hooks. When using a href in Vanilla JS it works fine because the webiste reloads, but in React the site dosn’t reloads and my hamburger menu dosn’t closes. I want my hamburger menu to close without having to reload my page. How can I close the hamburger menu when clicking on a link without reloading my page (using React hooks)?

import React from "react";
    import { FaGithub } from "react-icons/fa";
    import { Link } from "react-router-dom";
    
    const NavBar = () => {`enter code here`
      return (
        <>
          <div className="navbar-container">
            <div className="container">
              <nav>
                <input type="checkbox" id="nav" className="hidden" />
                <label htmlFor="nav" className="nav-btn">
                  <i></i>
                  <i></i>
                </label>
                <div className="logo">
                  <Link to="/">Logo</Link>
                </div>
                <div className="nav-wrapper">
                  <ul>
                    <li className="nav-item">
                      <Link to="/">Work</Link>
                    </li>
                    <li className="nav-item">
                      <Link style={{ textDecoration: "none" }} to="/contact">
                        contact
                      </Link>
                    </li>
                    <li className="nav-item">
                      <Link
                        to="https://github.com/sourmondays"
                        target="_blank"
                        rel="noreferrer"
                      >
                        <FaGithub />
                      </Link>
                    </li>
                  </ul>
                </div>
              </nav>
            </div>
          </div>
        </>
      );
    };
    
    export default NavBar;

CSS:

    .navbar-container {
      margin-bottom: 7rem;
      position: relative;
    }
    
    nav {
      padding: 1.5rem 2rem;
    }
    
    .logo {
      float: left;
      margin-left: 1rem;
      margin-top: 0.5rem;
    }
    
    * .logo a {
      letter-spacing: 0px;
      text-decoration: none;
      color: black;
      font-size: 2rem;
      font-family: "Oswald", sans-serif;
    }
    
    nav ul {
      float: right;
    }
    
    nav ul li {
      display: inline-block;
      padding-top: 1rem;
    }
    
    nav ul li:not(:first-child) {
      margin-left: 48px;
    }
    
    nav ul li:last-child {
      margin-right: 24px;
    }
    
    nav ul li a {
      display: inline-block;
      outline: none;
      color: black;
      text-transform: uppercase;
      text-decoration: none;
      letter-spacing: 1.2px;
      font-family: "Oswald", sans-serif;
      margin-top: 3.5px;
    }
    
    @media screen and (max-width: 864px) {
      .logo {
        padding: 0;
        padding-top: 1rem;
      }
    
      .logo a {
        font-size: 1.5rem;
      }
    
      .nav-btn {
        padding-top: 1rem;
      }
    
      .nav-wrapper {
        position: fixed;
        top: 0;
        right: 0;
        width: 100%;
        height: 100%;
        z-index: -1;
        background: #fff;
        opacity: 0;
      }
    
      .nav-wrapper ul {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        width: 100%;
      }
    
      .nav-wrapper ul li {
        display: block;
        float: none;
        width: 100%;
        text-align: right;
        margin-bottom: 10px;
        text-align: center;
      }
    
      .nav-wrapper ul li:not(:first-child) {
        margin-left: 0;
      }
    
      .nav-wrapper ul li a {
        padding: 10px 24px;
        opacity: 0;
        color: #000;
        font-size: 18px;
        letter-spacing: 1.2px;
        transition: all 0.2s ease;
      }
    
      .nav-btn {
        position: fixed;
        right: 3rem;
        display: block;
        cursor: pointer;
        z-index: 9998;
      }
    
      .nav-btn i {
        display: block;
        width: 25px;
        height: 2px;
        background: black;
      }
    
      .nav-btn i:nth-child(1) {
        margin-top: 18px;
      }
    
      .nav-btn i:nth-child(2) {
        margin-top: 6px;
      }
    }
    
    @media screen and (max-width: 375px) {
      .nav-wrapper {
        width: 100%;
      }
    }
    
    #nav:checked + .nav-btn i {
      background: #000;
      transition: transform 0.2s ease;
    }
    
    #nav:checked + .nav-btn i:nth-child(1) {
      transform: translateY(3px) rotate(-135deg);
    }
    
    #nav:checked + .nav-btn i:nth-child(2) {
      transform: translateY(-5px) rotate(135deg);
    }
    
    #nav:checked ~ .nav-wrapper {
      z-index: 9990;
      opacity: 1;
    }
    
    #nav:checked ~ .nav-wrapper ul li a {
      opacity: 1;
      transform: translateX(0);
      font-size: 6vmin;
    }
    
    .hidden {
      display: none;
    }



Source link

資料來源:Stackoverflow

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *