javascript - 如何在点击反应时隐藏div
问题描述
我有一个 divx
我需要隐藏整个 div 点击x
。我使用钩子 useState()。
.noShow {
display: none;
}
const [showed, setShowed] = useState(false)
const hideNav = () => {
setShowed(true)
}
<div className={ showed ? 'noShow' : ''} className='d-flex justify-content-between align-center pl-2 pr-2'>
<h6>this is div</h6>
<h6 onClick={hideNav}>x</h6>
</div>
如果我设置显示 - 最初它是隐藏的,但是当我点击它时x
它不起作用。
解决方案
好吧,当您单击隐藏时,您应该调用一个函数。试试这样。
import "./styles.css";
import { useState } from "react";
export default function App() {
const [showed, setShowed] = useState(false);
return (
<div
style={showed ? { display: "none" } : { display: "block" }}
className="d-flex justify-content-between align-center pl-2 pr-2"
>
<h6>this is div</h6>
<h6 onClick={(e) => setShowed(true)}>hide</h6>
</div>
);
}
你可以在这里确认。 https://codesandbox.io/s/thirsty-shadow-kz1mp?file=/src/App.js
推荐阅读
- javascript - 使用按位运算符删除小数部分如何工作?
- angular-test - 无法绑定到“ngModel”,因为它不是“quill-editor”的已知属性
- sql - 我需要显示财务部门的最低工资和最高工资之间的减法(其中department_id = 100)
- python - 调用前一个函数中定义的变量时显示错误值 (None)
- php - Symfony,WebTestCase 案例,由于测试中未使用的服务而崩溃
- node.js - Nodejs + Firebase 身份验证
- c - C中的函数和while循环问题:找不到我无法退出循环的原因
- amazon-s3 - aws s3 上的 Mp4 导致过度获取
- javascript - 当页面动态加载超过 20 个音频标签时,如何自动播放一个音频标签?
- three.js - 从 Blender 将 .obj 导入 Three.js - 模型中缺少孔