Disable Right Click in React.JS

落爺英雄遲暮 提交于 2020-08-06 08:40:28

问题


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.


回答1:


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

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


来源:https://stackoverflow.com/questions/35043748/disable-right-click-in-react-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!