简体   繁体   English

在反应中单击汉堡包图标时如何显示菜单列表

[英]How to display the menu list when clicking on the hamburger icon in react

I am learning react.js.我正在学习 react.js。 I have an issue with a menu click on mobile.我在移动设备上单击菜单时遇到问题。

I am displaying the Humbarger icon on the mobile.我在手机上显示 Humbarger 图标。 Now how can I display the close icon and menu list when the user clicks on the Humbarger icon?现在,当用户单击 Humbarger 图标时,如何显示关闭图标和菜单列表?

The second issue is ,第二个问题是

I am on the home page and I clicked on about us from the menu then my page is redirected to the about us but the issue is my menu is still showing open.我在主页上,我从菜单中单击了关于我们,然后我的页面被重定向到关于我们,但问题是我的菜单仍然显示为打开状态。 I have to close the menu so that users can the about page.我必须关闭菜单,以便用户可以访问关于页面。

(Step 1) (步骤1)

在此处输入图像描述

(Step 2)I click on Humbarger icon and menu open and I click on about us (第 2 步)我点击 Humbarger 图标并打开菜单,然后点击关于我们在此处输入图像描述

(Step 3) Now notice here, the page is redirect to the about us but the menu is stil open. (第 3 步)现在请注意,页面被重定向到 about us,但菜单仍然打开。 在此处输入图像描述

You can check my code here: https://codesandbox.io/s/happy-almeida-t6q7w?file=/src/components/Header.js你可以在这里查看我的代码: https://codesandbox.io/s/happy-almeida-t6q7w?file=/src/components/Header.js

Would you help me out with this issue?你能帮我解决这个问题吗?

What you could do is to add an event handler to the hamburger menu and close icon to update a local state like open or close .您可以做的是将事件处理程序添加到汉堡菜单并关闭图标以更新本地 state ,例如openclose Then depending one the state you add or remove a className.然后根据您添加或删除类名的 state 之一。 Like this:像这样:

Style.css款式.css

/* When we click the hamburger menu we want to hide the icon */
.hamburger_img.close {
    display: none;
}

/* When we click the menu we want to display this icon */
.right-menu.open {
    display: block;
}

HeaderMenu.js HeaderMenu.js

const HeaderMenu = () => {

    // Adding class name when scrolll
    const [openMenu, setOpenMenu] = useState(false);

    // Other code here..

    // A toggler to update the state when we open or close menu
    const toggleMenu = () => setOpenMenu(openMenu => !openMenu);

    // Dynamically add 'open' class if the state is true
    const getXMarkClassName = () => `right-menu float-right ${openMenu ? 'open': ''}`;

    // Dynamically add 'close' class if the state is true
    const getHamburgerMenuClassName = () => `hamburger_img ${openMenu ? 'close': ''}`;

    return (
        <header id="header_menu" className={headerClassName}>
            <div className={getHamburgerMenuClassName()} onClick={toggleMenu} >
                <img src={require("../images/menu.png")} alt="Menu bar"/>
            </div>
            <div className={getXMarkClassName()}>
                <div className="x_mark_img" onClick={toggleMenu} >
                     <img src={require("../images/close.png")} alt="Menu Close" />
               </div>
               <ul>
                   {/* code here... */}
               </ul>
            </div>
        </header>
  );
};

Notice that I added an onClick handler to the div to update the state whenever they are clicked.请注意,我在div中添加了一个onClick处理程序,以便在单击时更新 state。 Like wise notice that I call a function to get the className for both the icon menu and the close icon.明智地注意到,我调用className来获取图标菜单和关闭图标的类名。

Second Issue第二期

To close the navigation when the route changes you can listen to route changes using useEffect and then call the toggle() function.要在路由更改时关闭导航,您可以使用useEffect监听路由更改,然后调用toggle() function。 Like this:像这样:

import React, { useEffect } from 'react';
import { useLocation } from 'react-router';

function HeaderMenu() {
    // Other code here...
    const location = useLocation();

    useEffect(() => {
      console.log("route has been changed, toggle the menu");

      if (openMenu) {
          toggleMenu();
      }

      // To scroll up on route change
      window.scrollTo(0, 0);
  }, [location.pathname]);

  // Other code here...
}

Notice I didn't add openMenu to the list of dependencies in useEffect alongside location.pathname .请注意,我没有将openMenu添加到useEffectlocation.pathname的依赖项列表中。 This is because I don't want this useEffect to run anytime the openMenu state changes only when the route changes.这是因为我不希望这个 useEffect 在openMenu state 仅在路径更改时才更改时运行。 I have an if statement there so if the route changes and the menu wasn't opened, the toggle shouldn't be called.我在那里有一个 if 语句,因此如果路线发生变化并且菜单未打开,则不应调用切换。

Hope it helps.希望能帮助到你。 You can test it in this codesandbox您可以在此代码框中对其进行测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM