I'm new to React and Web-Dev in general. I'm creating a custom context menu when a user right-clicks on the web-app I'm creating. A modal should pop-up when you click on the custom context menu. However, the modal isn't showing up. What am I doing wrong?
Here is the code for the context menu:
class ContextMenu extends React.Component {
constructor(props){
super(props);
this.state = {
xPos: "0px",
yPos: "0px",
showMenu: false,
}
}
componentDidMount() {
document.addEventListener("click", this.handleClick);
document.addEventListener("contextmenu", this.handleContextMenu);
}
componentWillUnmount() {
document.removeEventListener("click", this.handleClick);
document.removeEventListener("contextmenu", this.handleContextMenu);
}
handleClick = (e) => {
if (this.state.showMenu) this.setState({ showMenu: false });
}
handleContextMenu = (e) => {
e.preventDefault();
this.setState({
xPos: `${e.pageX}px`,
yPos: `${e.pageY}px`,
showMenu: true,
})
}
alertClicked = () => {
<Modal show = {true}>
<Modal.Header closeButton>
<Modal.Title>Modal Title</Modal.Title>
</Modal.Header>
</Modal>
}
render() {
const { showMenu, yPos, xPos } = this.state;
if (showMenu) {
return (
<div
className="menu-container"
style={{
top: yPos,
left: xPos,
zIndex: 9,
position: "absolute",
}}>
<ListGroup>
<ListGroup.Item action onClick = {this.alertClicked}>
Show Modal
</ListGroup.Item>
</ListGroup>
</div>
);
} else return null;
}
}
This is the article I'm following to create the context menu.