reactjs - 菜单渲染列表更改 Y 位置
问题描述
我在做一个简单的菜单,稍微练习一下
如果我单击第二个选项,我会呈现 options2Option 列表,但它会在 Y 中更改
在没有任何 Y 运动的情况下如何做到这一点?(如果您删除,则相同<Global/>
)
我想知道它是否是关于某个绝对位置,内部相对但我还没有确定
import React, { useState } from 'react'
import styled, { createGlobalStyle } from 'styled-components'
export const Menu = () => {
const Global = createGlobalStyle`
body{
margin:0;
padding:0;
height:100vh;
display: flex;
align-items: center;
justify-content: center;
}
`
const Active = styled.div`
cursor: pointer;
width:60px;
`
const UL = styled.ul`
list-style: '- ';
display:${() => toggle || "none"};
`
const LI = styled.li`
padding:.1rem 0;
`
const [toggle, setToggle] = useState(false)
const handleToggle = () => {
setToggle(!toggle)
}
console.log(toggle)
const option2Options = [
'2.a opcion',
'2.b opcion',
'2.c opcion',
'2.d opcion',
'2.e opcion',
]
return (
<div>
<Global />
<div>Opcion 1</div>
<Active backgroundColor='red'
onClick={handleToggle}
>Opcion 2</Active>
<UL>
{
toggle &&
(
option2Options.map((element, index) => (
<LI key={index}>{element}</LI>
))
)
}
</UL>
<div>Opcion 3</div>
<div>Opcion 4</div>
</div>
)
}
提前致谢
解决方案
推荐阅读
- reactjs - MUI 组件上的 fullWidth 和 className
- python - 如何在python中处理hackerrank上的长字符串
- mysql - 如何验证数据源名称是否有效?
- python - 是否有使用 for 循环和 if else 的 Pythonic 列表理解?
- javascript - $lookup 中日期的时差
- python-3.x - 如何在 Python 3 中将 PyCairo 表面转换为 OpenCV numpy 并返回
- android - 布局背景不会根据 BottomSheet 布局中的可绘制背景进行裁剪
- ios - 如何在 MyApp.entitlements 中设置 com.apple.developer.associated-domains 的值?
- java - 将列和值的 HashMap 存储到 SQL 数据库以获取用户 ID
- android - BindingAdapter 真的节省了我们的时间吗