【发布时间】:2020-12-29 06:47:26
【问题描述】:
- 由于我是新手,所以我不太了解隐藏/显示。
- 在我的页面中,我有切换按钮,当我单击该按钮时,会出现一个包含 3 个选项的容器。
- 当我单击任一选项时,会出现一个警告框。
- 当我点击警告框中的确定按钮时,整个容器应该隐藏。
- 工作演示:codesandboxdemo
- 代码:
.App {
font-family: sans-serif;
text-align: center;
}
.style1 {
cursor: pointer;
padding-top: 10px;
border-bottom-left-radius: 1px
}
.style1:hover{
background-color: #D3D3D3;
}
.box{
background-color:#eeeeee;
width:20%;
margin-top:1px;
}
class Foo extends Component {
state = { showing: false, };
handleClick()
{
alert("hi");
}
render() {
const { showing } = this.state;
return (
<div>
<button onClick={() => this.setState({ showing: !showing })}>toggle</button>
{ showing
? (
<div className="box">
<div className="style1" onClick={this.handleClick}>Action1</div>
<div className="style1" onClick={this.handleClick}>Action2</div>
<div className="style1" onClick={this.handleClick}>Action3</div>
</div>
)
: null
}
</div>
)
}
}
export default Foo;
【问题讨论】: