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!
To distinguish left and right click events in React, we can set the onClick prop and onContextMenu to handle left click and right click events respectively. to add the handleClick function to handle both left and right click events. We check which type of click it is with the e. type propety.
Open the app -> Java -> Package -> Mainactivity.In this step, add the code to show the ContextMenu. Whenever the app will strat make a long click on a text and display the number of options to select of them for specific purposes. Comments are added inside the code to understand the code in more detail.
To open a link in a new tab in React, use the <a> element and set its target prop to _blank , e.g. <a href="https://google.com" target="_blank" rel="noopener noreferrer"></a> . The _blank value means that the resource is loaded into a new tab. Copied!
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.
There are few things to take care about with popup menus:
I created a library which you can use to accomplish all of these:
http://dkozar.github.io/react-data-menu/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With