在React.JS中禁用右键单击 [英] Disable Right Click in React.JS
问题描述
如何在ReactJS中禁用右键单击画布。这是我尝试的仍然无法使用的方法:
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.
警告:不建议从事件处理程序返回
false
,并且在以后的版本中将忽略该错误。而是视情况手动调用e.stopPropagation()或e.preventDefault()。
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.
编辑:是的,它确实停止了正确的操作单击画布上的单击功能,但是我的问题是:我在左键单击上画了一个点,并且也在右键单击上画了一个点,我想禁用它。
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.
推荐答案
此JS函数将防止contextmenu事件冒泡,从而防止出现上下文菜单:
This JS function will prevent bubbling of the contextmenu event, thus preventing the context menu from appearing:
canvas.oncontextmenu = function (e) {
e.preventDefault();
};
这篇关于在React.JS中禁用右键单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!