最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable Right Click in React.JS - Stack Overflow

programmeradmin1浏览0评论

How to disable right click in canvas in ReactJS. Here is what I tried which is still not working:

let Canvas = <canvas onContextMenu={(e)=>  {e.preventDefault(); return false;}} height={500} width={500} ref="canvas"/>;

A warning is also shown in browser console.

Warning: Returning false from an event handler is deprecated and will be ignored in a future release. Instead, manually call e.stopPropagation() or e.preventDefault(), as appropriate.

EDIT: Yes it did stop the right click functionality on Canvas, but my problem is: I am drawing a point on left click, and it is also being drawn on right click, I want to disable that.

How to disable right click in canvas in ReactJS. Here is what I tried which is still not working:

let Canvas = <canvas onContextMenu={(e)=>  {e.preventDefault(); return false;}} height={500} width={500} ref="canvas"/>;

A warning is also shown in browser console.

Warning: Returning false from an event handler is deprecated and will be ignored in a future release. Instead, manually call e.stopPropagation() or e.preventDefault(), as appropriate.

EDIT: Yes it did stop the right click functionality on Canvas, but my problem is: I am drawing a point on left click, and it is also being drawn on right click, I want to disable that.

Share Improve this question edited Aug 27, 2019 at 21:18 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Jan 27, 2016 at 17:11 VikramadityaVikramaditya 5,5747 gold badges36 silver badges46 bronze badges 1
  • Works fine for me – Brigand Commented Jan 27, 2016 at 17:49
Add a comment  | 

2 Answers 2

Reset to default 12

This JS function will prevent bubbling of the contextmenu event, thus preventing the context menu from appearing:

canvas.oncontextmenu = function (e) {
    e.preventDefault();
};

You could prevent the right-click from actually doing anything by simply ignoring it, like this:

handleMouseDown = e => {
  if (e.button === 0)
  {
    // Actions to perform when left mouse button is clicked, like update state
  }
}

let Canvas = <canvas onMouseDown={this.handleMouseDown} ...>;

发布评论

评论列表(0)

  1. 暂无评论