javascript – 使用React JS右键单击菜单

前端之家收集整理的这篇文章主要介绍了javascript – 使用React JS右键单击菜单前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道是否有一个最佳实践/正确的方式来设置React组件的右键单击菜单.

我目前有这个…

  1. // nw is nw.gui from Node-Webkit
  2. componentWillMount: function() {
  3. var menu = new nw.Menu();
  4. menu .append(new nw.MenuItem({
  5. label: 'doSomething',click: function() {
  6. // doSomething
  7. }
  8. }));
  9.  
  10. // I'd like to know if this bit can be done in a cleaner/more succinct way...
  11. // BEGIN
  12. var that = this;
  13. addEventListener('contextmenu',function(e){
  14. e.preventDefault();
  15. // Use the attributes property to uniquely identify this react component
  16. // (so different elements can have different right click menus)
  17. if (e.target.attributes[0].nodeValue == that.getDOMNode().attributes[0].nodeValue) {
  18. menu.popup(e.x,e.y);
  19. }
  20. })
  21. // END
  22. },

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

谢谢!

解决方法

更新:

看出来 – 这是你可以做的

  1. var addMenu;
  2.  
  3. componentWillMount: function() {
  4. addMenu = new nw.Menu();
  5. addMenu.append(new nw.MenuItem({
  6. label: 'doSomething',click: function() {
  7. // doSomething
  8. }
  9. }));
  10. },contextMenu: function(e) {
  11. e.preventDefault();
  12. addMenu.popup(e.clientX,e.clientY);
  13. },render: function(){
  14. return <button onClick={this.handleClick} onContextMenu={this.contextMenu}>SomethingUseful</button>
  15. }

在渲染中,您可以将函数传递给onContextMenu,以便在此反应组件发生右击时.

猜你在找的JavaScript相关文章