【问题标题】:React responsive navbar and hamburger menuReact 响应式导航栏和汉堡菜单
【发布时间】:2021-06-21 02:49:14
【问题描述】:

我需要让 React 响应式导航栏。我发现一些在 HTML 中有效,但在 React 中无效。我认为问题出在getElementById,因为 React 不支持它。

import React from "react";
import { Link } from "react-router-dom";
import "./index.css";

function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
function Header() {
  return (
    <nav>
      <div className="topnav" id="myTopnav">
        <a href="#home" className="active">
          Home
        </a>
        <a href="#news">News</a>
        <a href="#contact">Contact</a>
        <a href="#about">About</a>
        <a href="javascript:void(0);" className="icon" onclick={myFunction}>
          <i className="fa fa-bars"></i>
        </a>
      </div>
    </nav>
  );
}

export default Header;

这是我的项目正在使用的 CSS:

.topnav {
  overflow: hidden;
  background-color: black;
}

.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.topnav .icon {
  display: none;
}

@media screen and (max-width: 600px) {
  .topnav a {
    display: none;
  }
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {
    position: relative;
  }
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
}

我应该如何更新我的反应代码,以便在单击图标时出现响应式菜单?

【问题讨论】:

  • 粘贴您的完整代码
  • 我在答案中发布 css,因为 stackoverflow 不允许我发布太多代码
  • 我已经添加了答案
  • 更新了我的答案。请看一看。

标签: javascript reactjs


【解决方案1】:

更新

myFunction() 放在Header() 中,因为这是一个基于函数的组件。

这是Header 组件更改后的样子:

import React from "react";
import { Link } from "react-router-dom";
import "./index.css";

function Header() {
  const myFunction = () => {
    var x = document.getElementById("myTopnav");
    if (x.className === "topnav") {
      x.className += " responsive";
    } else {
      x.className = "topnav";
    }
  }

  return (
    <nav>
      <div className="topnav" id="myTopnav">
        <a href="#home" className="active">
          Home
        </a>
        <a href="#news">News</a>
        <a href="#contact">Contact</a>
        <a href="#about">About</a>
        <a href="javascript:void(0);" className="icon" onClick={myFunction}>
          <i className="fa fa-bars"></i>
        </a>
      </div>
    </nav>
  );
}

export default Header;

请访问此链接查看:Code Sandbox Live Demo

【讨论】:

【解决方案2】:

如果您添加 useState 挂钩来管理您的组件,而不是使用 javascript 直接修改 DOM,您可能会遇到更少的问题。

import React, { useState } from 'react';

const Header = () => {
  // set a variable 'responsive' to false. Update it using 'setResponsive'
  const [responsive, setResponsive] = useState(false);

  const toggleResponsive = () => {
    //take current value of responsive and flip it
    setResponsive(prev => !prev);
  }

  return (
    <nav>
      <div className={responsive ? 'topnav responsive' : 'topnav'}>
        <a href='#home'>Home</a>
        ...
        <a className='icon' onClick={toggleResponsive}>
          <i className="fa fa-bars"></i>
        </a>
      </div>
    </nav>
  );
}

【讨论】:

    猜你喜欢
    • 2022-11-11
    • 2022-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-18
    • 2021-10-20
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多