gpt4 book ai didi

reactjs - React MUI 菜单不关闭

转载 作者:行者123 更新时间:2023-12-05 03:31:28 26 4
gpt4 key购买 nike

我正在对 React Material 进行一些练习,但我遇到了菜单问题。

奇怪的是我有两个 IconButton 组件,如果我将设置 anchor 元素的 onClick 放在第一个 IconButton 上,一切正常。

如果我将设置 anchor 元素的 onClick 放在第二个 IconButton 上,菜单将永远不会关闭..

为什么会这样?以及如何解决?这是代码:

import React from 'react';
import AppBar from '@mui/material/AppBar';
import Toolbar from '@mui/material/Toolbar';
import Avatar from '@mui/material/Avatar';
import Menu from '@mui/material/Menu';
import MenuItem from '@mui/material/MenuItem';

import IconButton from '@mui/material/IconButton';

import MenuIcon from '@mui/icons-material/Menu';
import AccountIcon from '@mui/icons-material/AccountCircle';

function TopComp(props) {
const [anchorEl, setAnchorEl] = React.useState(null);
const isMenuOpen = Boolean(anchorEl);

const handleMenuClick = (event) => {
setAnchorEl(event.currentTarget);
};

const handleMenuClose = () => {
setAnchorEl(null);
};

return (
<div style={{ marginBottom: '30px' }}>
<AppBar position="static" color="primary">
<Toolbar>
<IconButton
size="large"
edge="start"
color="inherit"
aria-label="menu"
sx={{ mr: 2 }}
//onClick={(event) => handleMenuClick(event)}
>
<MenuIcon />
</IconButton>

<div style={{ marginLeft: 'auto' }}>
<IconButton
size="large"
edge="end"
color="inherit"
aria-label="menu"
sx={{ mr: 2 }}
onClick={(event) => handleMenuClick(event)}
>
<Menu
id="basic-menu"
anchorEl={anchorEl}
open={isMenuOpen}
onClose={handleMenuClose}
>
<MenuItem onClick={handleMenuClose}>Profile</MenuItem>
<MenuItem onClick={handleMenuClose}>My account</MenuItem>
<MenuItem onClick={handleMenuClose}>Logout</MenuItem>
</Menu>
<Avatar>
<AccountIcon />
</Avatar>
</IconButton>
</div>
</Toolbar>
</AppBar>
</div>
);
}

export default TopComp;

最佳答案

将您的 Menu 组件放在 IconButton 之外:-

import React from 'react';
import AppBar from '@mui/material/AppBar';
import Toolbar from '@mui/material/Toolbar';
import Avatar from '@mui/material/Avatar';
import Menu from '@mui/material/Menu';
import MenuItem from '@mui/material/MenuItem';

import IconButton from '@mui/material/IconButton';

import MenuIcon from '@mui/icons-material/Menu';
import AccountIcon from '@mui/icons-material/AccountCircle';

function TopComp(props) {
const [anchorEl, setAnchorEl] = React.useState(null);
const isMenuOpen = Boolean(anchorEl);

const handleMenuClick = (event) => {
setAnchorEl(event.currentTarget);
};

const handleMenuClose = () => {
setAnchorEl(null);
};

return (
<div style={{ marginBottom: '30px' }}>
<AppBar position="static" color="primary">
<Toolbar>
<IconButton
size="large"
edge="start"
color="inherit"
aria-label="menu"
sx={{ mr: 2 }}
onClick={(event) => handleMenuClick(event)}
>
<MenuIcon />
</IconButton>

<Menu
id="basic-menu"
anchorEl={anchorEl}
open={isMenuOpen}
onClose={handleMenuClose}
>
<MenuItem onClick={handleMenuClose}>Profile</MenuItem>
<MenuItem onClick={handleMenuClose}>My account</MenuItem>
<MenuItem onClick={handleMenuClose}>Logout</MenuItem>
</Menu>

<div style={{ marginLeft: 'auto' }}>
<IconButton
size="large"
edge="end"
color="inherit"
aria-label="menu"
sx={{ mr: 2 }}
onClick={(event) => handleMenuClick(event)}
>
<Avatar>
<AccountIcon />
</Avatar>
</IconButton>
</div>
</Toolbar>
</AppBar>
</div>
);
}

export default TopComp;

On codesandbox.io

关于reactjs - React MUI 菜单不关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70644145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com