无法在React中切换下拉列表

huangapple go评论64阅读模式
英文:

unable to toggle dropdown list in react

问题

我已经创建了一个简单的React项目,试图在其中实现下拉列表。

我正在尝试在React中为个人资料创建一个下拉列表,就像这样:

dropdown.js

import React, { useState } from "react";
import "./style.css";

function DropdownNavbar() {
  const [toggle, setToggle] = useState(false);

  const menuToggle = () => {
    setToggle(!toggle);
  };

  return (
    <>
      <div className="new-box d-flex" onClick={menuToggle}>
        <div className="profile p-2 flex-fill">
          <img src="images/avatar-img.png" />
        </div>
        <div className="name-text p-2 flex-fill">Alex John</div>
      </div>
      {toggle && (
        <div class="menu">
          <ul>
            <li>
              <a href="#">My profile</a>
            </li>
            <li>
              <a href="#">Inbox</a>
            </li>
            <li>
              <a href="#">Logout</a>
            </li>
          </ul>
        </div>
      )}
    </>
  );
}

export default DropdownNavbar;

style.css

.new-box{
  display: flex;
}

.name-text{
  margin-top: 15px;
}

.profile {
  position: relative;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  cursor: pointer;
  margin: 5px 10px 5px 0px;
}

.profile img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.menu {
  position: absolute;
  top: 120px;
  right: -10px;
  padding: 10px 20px;
  background: #fff;
  width: 200px;
  box-sizing: 0 5px 25px rgba(0, 0, 0, 0.1);
  border-radius: 15px;
  transition: 0.5s;
  visibility: hidden;
  opacity: 0;
}

.menu.active {
  top: 80px;
  visibility: visible;
  opacity: 1;
}

.menu::before {
  content: "";
  position: absolute;
  top: -5px;
  right: 28px;
  width: 20px;
  height: 20px;
  background: #fff;
  transform: rotate(45deg);
}

.menu h3 {
  width: 100%;
  text-align: center;
  font-size: 18px;
  padding: 20px 0;
  font-weight: 500;
  color: #555;
  line-height: 1.5em;
}

.menu h3 span {
  font-size: 14px;
  color: #cecece;
  font-weight: 300;
}

.menu ul li {
  list-style: none;
  padding: 16px 0;
  border-top: 1px solid rgba(0, 0, 0, 0.05);
  display: flex;
  align-items: center;
}

.menu ul li img {
  max-width: 20px;
  margin-right: 10px;
  opacity: 0.5;
  transition: 0.5s;
}

.menu ul li:hover img {
  opacity: 1;
}

.menu ul li a {
  display: inline-block;
  text-decoration: none;
  color: #555;
  font-weight: 500;
  transition: 0.5s;
}

.menu ul li:hover a {
  color: #ff5d94;
}

我将此个人资料部分添加到我的导航栏。导航栏在App.js中呈现。当我单击div时,没有任何反应。有人可以帮忙吗?

英文:

i have created a simple react project and i was trying to implement drop down list in it.

i am trying to create a dropdown list for profile in react like this->

dropdown.js

   import React, { useState } from &quot;react&quot;;
import &quot;./style.css&quot;;
function DropdownNavbar() {
const [toggle, setToggle] = useState(false);
const menuToggle = () =&gt; {
setToggle(!toggle);
};
return (
&lt;&gt;
&lt;div className=&quot;new-box d-flex&quot; onClick={menuToggle}&gt;
&lt;div className=&quot;profile p-2 flex-fill&quot;&gt;
&lt;img src=&quot;images/avatar-img.png&quot; /&gt;
&lt;/div&gt;
&lt;div className=&quot;name-text p-2 flex-fill&quot;&gt;Alex John&lt;/div&gt;
&lt;/div&gt;
{toggle &amp;&amp; (
&lt;div class=&quot;menu&quot;&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot;&gt;My profile&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot;&gt;Inbox&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot;&gt;Logout&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
)}
&lt;/&gt;
);
}
export default DropdownNavbar;

style.css

.new-box{
display: flex;
}
.name-text{
margin-top: 15px;
}
.profile {
position: relative;
width: 40px;
height: 40px;
border-radius: 50%;
overflow: hidden;
cursor: pointer;
margin: 5px 10px 5px 0px;
}
.profile img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
}
.menu {
position: absolute;
top: 120px;
right: -10px;
padding: 10px 20px;
background: #fff;
width: 200px;
box-sizing: 0 5px 25px rgba(0, 0, 0, 0.1);
border-radius: 15px;
transition: 0.5s;
visibility: hidden;
opacity: 0;
}
.menu.active {
top: 80px;
visibility: visible;
opacity: 1;
}
.menu::before {
content: &quot;&quot;;
position: absolute;
top: -5px;
right: 28px;
width: 20px;
height: 20px;
background: #fff;
transform: rotate(45deg);
}
.menu h3 {
width: 100%;
text-align: center;
font-size: 18px;
padding: 20px 0;
font-weight: 500;
color: #555;
line-height: 1.5em;
}
.menu h3 span {
font-size: 14px;
color: #cecece;
font-weight: 300;
}
.menu ul li {
list-style: none;
padding: 16px 0;
border-top: 1px solid rgba(0, 0, 0, 0.05);
display: flex;
align-items: center;
}
.menu ul li img {
max-width: 20px;
margin-right: 10px;
opacity: 0.5;
transition: 0.5s;
}
.menu ul li:hover img {
opacity: 1;
}
.menu ul li a {
display: inline-block;
text-decoration: none;
color: #555;
font-weight: 500;
transition: 0.5s;
}
.menu ul li:hover a {
color: #ff5d94;
}

i am adding this profile thing in my navbar. navbar is being rendered in App.js
when i click on div nothing happens. can someone help here?

答案1

得分: 0

有错误的处理程序

应该是:

onClick={menuToggle}

不要使用class - 使用className,而不是直接DOM - 使用useState hook。
示例:

const [isOpen, setIsOpen] = useState(false);
const menuToggle = () => {
  setIsOpen((prev) => !prev);
};
<div className={isOpen ? "menu active" : "menu"}>
//...

https://codesandbox.io/s/kind-rgb-wlwlgf

英文:

Mistake in handler

It should be:

onClick={menuToggle}

And dont use class - use className, not directly DOM - use useState hook.
Example:

const [isOpen, setIsOpen] = useState(false);
const menuToggle = () =&gt; {
setIsOpen((prev) =&gt; !prev);
};
&lt;div className={isOpen ? &quot;menu active&quot; : &quot;menu&quot;}&gt;
//...

https://codesandbox.io/s/kind-rgb-wlwlgf

答案2

得分: 0

found a better way to implement drop down (on hover) instead of onclick.

Dropdown.js

import React from "react";
import { AiFillCaretDown } from "react-icons/ai";
import "./style.css";

function DropdownNavbar() {
  return (
    <div className="dropdown">
      <button className="dropbtn">
        <div className="new-box d-flex">
          <div className="p-2 flex-fill">
            <img src="images/avatar-img.png" />
          </div>
          <div
            style={{ marginTop: "5px", marginLeft: "10px" }}
            className="p-2 flex-fill"
          >
            <span className="dropdown-text">Alex John</span>
          </div>
          <div
            style={{ marginTop: "8px", marginLeft: "10px" }}
            className="p-2 flex-fill"
          >
            <AiFillCaretDown />
          </div>
        </div>
      </button>
      <div className="dropdown-content">
        <a href="#">Profile</a>
        <a href="#">Help</a>
        <a href="#">Logout</a>
      </div>
    </div>
  );
}

export default DropdownNavbar;

style.css

.dropdown {
  float: left;
  overflow: hidden;
  margin-top: 5px;
}

.dropdown img {
  width: 40px;
  height: 40px;
}

.dropdown-text {
  color: Black;
}

.new-box {
  display: flex;
}

.dropdown .dropbtn {
  margin-right: 30px;
  font-size: 16px;
  border: none;
  outline: none;
  background-color: inherit;
  font-family: inherit;
}

.dropdown:hover .dropbtn {
  background-color: #d8d8d8;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: #d8d8d8;
}

.dropdown:hover .dropdown-content {
  display: block;
}
英文:

found a better way to implement drop down (on hover) instead of onclick.

Dropdown.js

import React from &quot;react&quot;;
import { AiFillCaretDown } from &quot;react-icons/ai&quot;;
import &quot;./style.css&quot;;
function DropdownNavbar() {
return (
&lt;div className=&quot;dropdown&quot;&gt;
&lt;button className=&quot;dropbtn&quot;&gt;
&lt;div className=&quot;new-box d-flex&quot;&gt;
&lt;div className=&quot;p-2 flex-fill&quot;&gt;
&lt;img src=&quot;images/avatar-img.png&quot; /&gt;
&lt;/div&gt;
&lt;div
style={{ marginTop: &quot;5px&quot;, marginLeft: &quot;10px&quot; }}
className=&quot;p-2 flex-fill&quot;
&gt;
&lt;span className=&quot;dropdown-text&quot;&gt;Alex John&lt;/span&gt;
&lt;/div&gt;
&lt;div
style={{ marginTop: &quot;8px&quot;, marginLeft: &quot;10px&quot; }}
className=&quot;p-2 flex-fill&quot;
&gt;
&lt;AiFillCaretDown /&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;div className=&quot;dropdown-content&quot;&gt;
&lt;a href=&quot;#&quot;&gt;Profile&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Help&lt;/a&gt;
&lt;a href=&quot;#&quot;&gt;Logout&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;
);
}
export default DropdownNavbar;

style.css

.dropdown {
float: left;
overflow: hidden;
margin-top: 5px;
}
.dropdown img {
width: 40px;
height: 40px;
}
.dropdown-text {
color: Black;
}
.new-box {
display: flex;
}
.dropdown .dropbtn {
margin-right: 30px;
font-size: 16px;
border: none;
outline: none;
background-color: inherit;
font-family: inherit;
}
.dropdown:hover .dropbtn {
background-color: #d8d8d8;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #d8d8d8;
}
.dropdown:hover .dropdown-content {
display: block;
}

huangapple
  • 本文由 发表于 2023年2月14日 21:30:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定