我试图将右键单击菜单添加到菜单项。我想我已经管理好了,但是在右键单击菜单中单击某个项目后,菜单项的单击操作也会触发。任何想法?示例:https://codesandbox.io/s/q8nl4ypq49
编辑:我的react组件代码
import React from "react";
import ReactDOM from "react-dom";
import Popup from "./Popup";
import Table from "antd/lib/table";
import Dropdown from "antd/lib/dropdown";
import Menu from "antd/lib/menu";
import { columns, data } from "./data";
import "antd/dist/antd.css";
const menu = (
<Menu>
<Menu.Item
onClick={e => {
alert("clicked");
}}
>
click
</Menu.Item>
<Menu.Item>Like it</Menu.Item>
<Menu.Item>Bookmark</Menu.Item>
</Menu>
);
class App extends React.Component {
render() {
return (
<div>
<Menu mode="horizontal">
<Menu.Item
key="1"
onClick={() => {
alert("parent clicked");
}}
>
<Dropdown overlay={menu} trigger={[`contextMenu`]}>
<span style={{ userSelect: "none" }}> right click </span>
</Dropdown>
</Menu.Item>
</Menu>
</div>
);
}
}
const APP_NODE = document.getElementById(`container`);
ReactDOM.render(<App />, APP_NODE);
答案 0 :(得分:1)
您需要停止在子元素上传播,因为在JavaScript中,单击事件使DOM树冒泡。
const menu = (
<Menu>
<Menu.Item
onClick={e => {
e.domEvent.stopPropagation();
alert("clicked");
}}
click
</Menu.Item>
<Menu.Item >Like it</Menu.Item>
<Menu.Item>Bookmark</Menu.Item>
</Menu>
);