'How are Buttons and Popovers connected when using MUI and ReactJS?
I am trying to set up three Popovers opened from 3 different buttons. Currently all three buttons will open the different popovers at the same time. I have tried looking this problem up but the answers do not seem to make sense to me or don't seem very clean. Please see the following example:
import * as React from "react";
import Popover from "@mui/material/Popover";
import Typography from "@mui/material/Typography";
import Button from "@mui/material/Button";
export default function BasicPopover() {
const [anchorEl, setAnchorEl] = React.useState(null);
const handleClick = (event) => {
setAnchorEl(event.currentTarget);
};
const handleClose = () => {
setAnchorEl(null);
};
const open = Boolean(anchorEl);
const id = open ? "simple-popover" : undefined;
return (
<div>
<Button aria-describedby={id} variant="contained" onClick={handleClick}>
Chocolate!
</Button>
<Popover
id={id}
open={open}
anchorEl={anchorEl}
onClose={handleClose}
anchorOrigin={{
vertical: "bottom",
horizontal: "left"
}}
>
<Typography sx={{ p: 2 }}>Chocolate</Typography>
</Popover>
<Button aria-describedby={id} variant="contained" onClick={handleClick}>
Vanilla!
</Button>
<Popover
id={id}
open={open}
anchorEl={anchorEl}
onClose={handleClose}
anchorOrigin={{
vertical: "bottom",
horizontal: "left"
}}
>
<Typography sx={{ p: 2 }}>Vanilla</Typography>
</Popover>
</div>
);
}
If anyone could help me or lead me in the right direction, it would be greatly appreciated!
Solution 1:[1]
The components are named and triggered by the same values, and will trigger together.
I have just duplicated the values that need to have some type of separation to show you how it works.
import * as React from "react";
import { Typography, Popover, Button } from '@material-ui/core';
export default function BasicPopover() {
const [anchorEl, setAnchorEl] = React.useState(null);
const [anchorEl2, setAnchorEl2] = React.useState(null);
const handleClick = (event) => {
setAnchorEl(event.currentTarget);
};
const handleClick2 = (event) => {
setAnchorEl2(event.currentTarget);
};
const handleClose = () => {
setAnchorEl(null);
};
const handleClose2 = () => {
setAnchorEl2(null);
};
const open = Boolean(anchorEl);
const open2 = Boolean(anchorEl2);
const id = open ? "simple-popover" : undefined;
const id2 = open2 ? "simple-popover2" : undefined;
return (
<div>
<Button aria-describedby={id} variant="contained" onClick={handleClick}>
Chocolate!
</Button>
<Popover
id={id}
open={open}
anchorEl={anchorEl}
onClose={handleClose}
anchorOrigin={{
vertical: "bottom",
horizontal: "left"
}}
>
<Typography sx={{ p: 2 }}>Chocolate</Typography>
</Popover>
<Button aria-describedby={id2} variant="contained" onClick={handleClick2}>
Vanilla!
</Button>
<Popover
id={id2}
open={open2}
anchorEl={anchorEl2}
onClose={handleClose2}
anchorOrigin={{
vertical: "bottom",
horizontal: "left"
}}
>
<Typography sx={{ p: 2 }}>Vanilla</Typography>
</Popover>
</div>
);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | kaws84 |
