微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 使用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,以便在此反应组件发生右击时.

原文地址:https://www.jb51.cc/js/152843.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐