[英]I'm having a problem triggering this classlist toggle in React
I am trying to trigger this CSS open in React the tutorial was in HTML but I cannot use addeventlistner or use document.queryselector so I added an onClick event to the hamburger which should handle both of those issues now I just need to toggle this Class.Please do not mark this as already answered because the answered questions are jquery and regular html related.我试图在 React 中触发这个 CSS 打开,教程是在 HTML 中,但我不能使用 addeventlistner 或使用 document.queryselector 所以我向汉堡包添加了一个 onClick 事件,它应该处理这两个问题,现在我只需要切换这个类。请不要将此标记为已回答,因为回答的问题与 jquery 和常规 html 相关。
Component:成分:
const Navbar = props => {
const hamburger = document.querySelector('.hamburger');
const navLinks = document.querySelector('.nav-links');
const links = document.querySelectorAll('nav-links li');
// hamburger.addEventListener('click', () => {
// navLinks.classList.toggle("open")
// });
useEffect(() => {
}, []);
return (
<div>
<nav>
<div onCLick={//I want to toggle the open class on click} className="hamburger">
<div className="line"></div>
<div className="line"></div>
<div className="line"></div>
</div>
<ul className="nav-links">
<li className="nav-links">About</li>
<li className="nav-links">About</li>
<li className="nav-links">About</li>
</ul>
</nav>
</div>
);
};
export default Navbar;
CSS: CSS:
nav{
height: 10vh;
background: blue !important;
}
.nav-links{
display: flex;
list-style: none;
width: 50%;
height: 100%;
justify-content: space-around;
align-items: center;
margin-left: auto;
}
/* change to classes */
.nav-links li a{
color: white;
text-decoration: none;
font-size: 16px
}
.landing{
height: 90vh;
display: flex;
justify-content: center;
align-items: center;
}
.landing h1{
margin: 100px;
font-size: 50px;
}
@media screen and (max-width: 768px){
.nav-links{
position: fixed;
background: red;
height: 100vh;
width: 100%;
flex-direction: column;
display: none;
clip-path: circle(100px at 90% 10%);
-webkit-clip-path: circle(100px at 90% 10%);
z-index: 99999;
transition: all 1s ease-out
}
.nav-links.open{
clip-path: circle(1000px at 90% 10%);
-webkit-clip-path: circle(1000px at 90% 10%);
}
.line{
width: 30px;
height: 3px;
background: white;
margin: 5px
}
nav{
position: relative
}
.hamburger{
position: absolute;
cursor: pointer;
right: 5%;
top: 50%;
transform: translate(-5%,-50%)
}
}
You should employ state
which is React's way of helping you keep track of the current status of your component (Like when your hamburger is clicked).你应该使用
state
,这是 React 帮助你跟踪组件当前状态的方式(比如当你的汉堡被点击时)。 One of the core concepts behind React is that you define event-listeners for your JSX. React 背后的核心概念之一是为 JSX 定义事件侦听器。 So when a user interacts with your application, an event is triggered and thus the
state
of your component changes.因此,当用户与您的应用程序交互时,会触发一个事件,从而更改组件的
state
。
We use state
to help construct your desired functionality.我们使用
state
来帮助构建您想要的功能。 In this case, clicking on the hamburger would toggle the open state
and thus we use that state
Boolean value to help us toggle the classes of your nav-links list.在这种情况下,单击汉堡包将切换
open state
,因此我们使用该state
布尔值来帮助我们切换导航链接列表的类。
There is no need to forcefully interact with the DOM
as you would with typical document selectors in vanilla JavaScript.不需要像使用普通 JavaScript 中的典型文档选择器那样强制与
DOM
交互。
See working sandbox: https://codesandbox.io/s/cool-wildflower-94e6q查看工作沙箱: https : //codesandbox.io/s/cool-wildflower-94e6q
import React, { useState, useEffect } from "react";
import ReactDOM from "react-dom";
import "./styles.css";
const Navbar = props => {
const [open, setOpen] = useState(false);
return (
<div>
<nav>
<div onClick={() => setOpen(!open)} className="hamburger">
<div className="line" />
<div className="line" />
<div className="line" />
</div>
<ul className={open ? "nav-links" : "hide"}>
<li className="nav-links">About</li>
<li className="nav-links">About</li>
<li className="nav-links">About</li>
</ul>
</nav>
</div>
);
};
export default Navbar;
const rootElement = document.getElementById("root");
ReactDOM.render(<Navbar />, rootElement);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.