使用React JS右键单击菜单

编程入门 行业动态 更新时间:2024-10-27 04:32:22
本文介绍了使用React JS右键单击菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道是否有最佳实践/正确的方法来为React组件设置右键菜单.

我目前有这个...

// nw is nw.gui from Node-Webkit componentWillMount: function() { var menu = new nw.Menu(); menu .append(new nw.MenuItem({ label: 'doSomething', click: function() { // doSomething } })); // I'd like to know if this bit can be done in a cleaner/more succinct way... // BEGIN var that = this; addEventListener('contextmenu', function(e){ e.preventDefault(); // Use the attributes property to uniquely identify this react component // (so different elements can have different right click menus) if (e.target.attributes[0].nodeValue == that.getDOMNode().attributes[0].nodeValue) { menu.popup(e.x, e.y); } }) // END },

这可行,但是感觉有点混乱,我想知道是否还有我可以使用的另一种方法,任何信息将不胜感激,

谢谢!

解决方案

更新:

想通了-这是您可以做的

var addMenu; componentWillMount: function() { addMenu = new nw.Menu(); addMenu.append(new nw.MenuItem({ label: 'doSomething', click: function() { // doSomething } })); }, contextMenu: function(e) { e.preventDefault(); addMenu.popup(e.clientX, e.clientY); }, render: function(){ return <button onClick={this.handleClick} onContextMenu={this.contextMenu}>SomethingUseful</button> }

在渲染中,您可以将一个函数传递给onContextMenu,以使该React组件发生右键单击.

I'd like to know if there is a best practice/correct way to setup a right-click menu for a React component.

I currently have this...

// nw is nw.gui from Node-Webkit componentWillMount: function() { var menu = new nw.Menu(); menu .append(new nw.MenuItem({ label: 'doSomething', click: function() { // doSomething } })); // I'd like to know if this bit can be done in a cleaner/more succinct way... // BEGIN var that = this; addEventListener('contextmenu', function(e){ e.preventDefault(); // Use the attributes property to uniquely identify this react component // (so different elements can have different right click menus) if (e.target.attributes[0].nodeValue == that.getDOMNode().attributes[0].nodeValue) { menu.popup(e.x, e.y); } }) // END },

This works, but it feels a little messy and I was wondering if there was another approach I might be able to use, any information would be greatly appreciated,

Thanks!

解决方案

UPDATE:

Figured it out - here is what you can do

var addMenu; componentWillMount: function() { addMenu = new nw.Menu(); addMenu.append(new nw.MenuItem({ label: 'doSomething', click: function() { // doSomething } })); }, contextMenu: function(e) { e.preventDefault(); addMenu.popup(e.clientX, e.clientY); }, render: function(){ return <button onClick={this.handleClick} onContextMenu={this.contextMenu}>SomethingUseful</button> }

In render you can pass a function to onContextMenu for when a right click occurs for this react component.

更多推荐

使用React JS右键单击菜单

本文发布于:2023-08-06 05:10:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1310264.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:右键   单击   菜单   React   JS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!