1

I am developing React js app with Material UI. I have implemented onClick in List and List Item. enter image description here I want to implement onRightClick event as well. Please help me.

L.Benrong
  • 81
  • 1
  • 1
  • 4

4 Answers4

6

Use onContextMenu event where you want to show your default context menu, not browser context menu.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Arman Omidi
  • 132
  • 1
  • 6
4

there is onContextMenu event in react defined events. if you want to change the rightClick action on your component or part of your component you need to write a custom eventHandler for onContextMenu event.

<div onContextMenu={this.someEvenetHandler}></div>

someEventHandler = (e) => {
      "Do stuff here"
}

or something like that.

Arman Omidi
  • 132
  • 1
  • 6
1

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react-dom.min.js"></script>
<html>
  <head>
    <title>Test</title>
  </head>
  <body>
    <div id="app"></div>
    <script type="text/jsx">
      var App = React.createClass({
       handleClick: function(e) {
          if (e.type === 'click') {
            console.log('Left click');
          } else if (e.type === 'contextmenu') {
            console.log('Right click');
          }
       },
       render : function(){
           return <p onClick={this.handleClick} onContextMenu={this.handleClick} >Something </p>
       }
    });

      React.render(<App />, document.getElementById('app'));
    </script>
  </body>
</html>

Try running above snippet you will be able to identify left and right click respectively.

Harsh Makadia
  • 3,263
  • 5
  • 30
  • 42
1

According to the react docs, onContextMenu is the correct prop to pass. To prevent the standard functionality, you probably need to add e.preventDefault(); to your callback function.

Bruce Smith
  • 811
  • 8
  • 14