2

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.

4 Answers 4

8

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.