.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;
Final output:
Here is how you can do that, I am using confirm
dialogue box instead of alert as it has an option for OK
and Cancel
both, so it will be more user friendly:
import React, { Component } from "react";
import "./styles.css";
class Foo extends Component {
state = { showing: false };
handleClick = () => {
if (window.confirm("Hide Div Emelent")) {
this.setState({ showing: false });
}
};
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;
Working Example: Codesandbox