reactjs - 我们可以使用 react native 为整个班级做隐藏和显示吗
问题描述
在我的 App.js 中,我有两个类。因此,当我单击一个类中的添加按钮时,应通过隐藏第一个类视图来显示第二个类视图。
解决方案
如果你在谈论类,那么你可以这样做:
const Foo = () => {
const [showClass, setShowClass] = useState(false);
return (
<div>
<div className={showClass ? "showClass" : "hideClass">
Only show after click
</div>
<div className={!showClass ? "showClass" : "hideClass"}>
Show when not clicked
</div>
<button onClick={() => setShowClass(true)}>Click me</button>
</div>
)
};
然而,在反应中,约定是不使用类,而是进行条件渲染:
const Foo = () => {
const [showElement, setShowElement] = useState(false);
return (
<div>
{
showElement ?
<div>
Only show after click
</div>
:
<div>
Show when not clicked
</div>
}
<button onClick={() => setShowElement(true)}>Click me</button>
</div>
)
};
推荐阅读
- java - 检测动画何时结束(Android Studio)
- angular - 使用 strictNullChecks 从可能为 null 的属性中键入别名
- html - Bootstrap 4 修复导航栏的问题
- php - 如何为 PATHINFO() 添加 readline 函数(扩展)
- node.js - 如何调用另一个类的函数或导出的对象?
- android - Firebase 实时数据库 - ChildEventListener - 基础
- python - 是否有一个 API 可以仅从其名称 (AppName) 中获取 Steam AppID?
- go - 使用以编程方式编写并连接到 os.stdin 的标准输入执行命令
- azure - 使用 webjobs 从 Azure 上的 SQL Server 数据库中删除记录的脚本
- java - Coverity:CSRF 已从 Spring Boot Security 中禁用,但问题未从 Coverity 中解决