<p><br /></p>
<pre class="brush:php;toolbar:false;"><Drawer
anchor="left"
className="drawerArea left-navigate"
variant="permanent"
open={true}
PaperProps={{
style: {
width: "inherit",
background: "#172B4D",
backgroundImage: `linear-gradient(to right,#172B4D 60% , white 40%)`,
border: "none",
overflowY: "hidden",
},
}}
>
<Box>
<Box>
<Box>
<Box className="listing" sx={{ width: "100%" }}>
<List className="mp-0 wh-100 overflow-overlay">
{menus &&
menus.length > 0 &&
menus.map((item, index) => (
<Box key={index}>
<ListItem
ref={popoverAnchor}
className={`mp-0 navigationList flex-start-center`}
** onMouseEnter={(e) => {
setNestedArr([...item?.children]);
popoverEnter(e);
setAnchorEl(e?.currentTarget);
}}
onMouseLeave={(e) => {
popoverLeave(e);
}}**
onClick={(e) => {
console.log(item,'parent_clicked')
}}
>
<ListItemText>
<Box>
{micons[item?.name] ? (
micons[item?.name]
) : (
<Icon>{item?.icon_class}</Icon>
)}
</Box>
<Typography>
{item?.name}
</Typography>
</Typography>
}
/>
</ListItem>
</Box>
))}
</List>
</Box>
</Box>
</Box>
{open && nestedArr.length ? (
<NestedMenu
open={open}
setOpen={(val) => setOpen(val)}
anchorEl={anchorEl}
nestedArr={nestedArr}
lightBackground={lightBackground}
secondaryColor={secondaryColor}
popoverAnchor={popoverAnchor}
popoverEnter={popoverEnter}
popoverLeave={popoverLeave}
/>
) : null}
</Box>
</Drawer></pre>
<p><strong>页面左侧有一些菜单(使用MUI库和material-ui-nested-menu-item库),当鼠标悬停在Listitems上时,它会显示一个带有嵌套或子菜单的菜单,并在鼠标离开时关闭菜单,但我面临的问题是在悬停在菜单上时同时触发了onMouseLeave事件,所以菜单不会打开。我无法找出问题所在。</strong></p>
在React中处理嵌套元素和鼠标事件时,您描述的问题是常见的。当您有嵌套元素并将onMouseEnter和onMouseLeave事件附加到父元素时,即使鼠标仍在父元素的范围内,移动鼠标到子元素上也会触发父元素的onMouseLeave事件。这是因为鼠标已经“离开”了父元素并“进入”了子元素。
为了解决这个问题,您可以使用onMouseOver和onMouseOut事件来替代onMouseEnter和onMouseLeave。onMouseOver和onMouseOut事件不会以相同的方式冒泡,因此它们可以帮助避免这个问题。