简体   繁体   English

在ReactJS中禁用ContextMenu

[英]Disable ContextMenu in ReactJS

first post here so hopefully I can ask this question the most helpful manner possible. 首先发布在这里,希望我能以最有用的方式提出这个问题。 I'm pretty new to coding and in trying to push myself decided to attempt to recreate Minesweeper using React and not using any tutorial. 我对编码很新,并试图推动自己决定尝试使用React重新创建Minesweeper而不使用任何教程。 I've gotten a lot of the functionality but am really stuck on this part. 我已经获得了很多功能,但我确实坚持这一部分。 I am using the event listener 'onContextMenu' to register a right click to "flag" a mine in the program. 我正在使用事件监听器'onContextMenu'来注册右键以“标记”程序中的一个地雷。 But I can't figure the right way to isolate it or maybe it's a syntax issue preventing the menu from popping up at the same time. 但我无法找到隔离它的正确方法,或者这可能是一个语法问题,导致菜单无法同时弹出。 In JS it seems really simple of just returning false on the event listener, but I can't figure it out in React. 在JS中,在事件监听器上返回false似乎很简单,但我无法在React中弄明白。

I'm currently using 'onContextMenu' to handle my right click and calling a function that handles the flag assignment with that event listener. 我正在使用'onContextMenu'来处理我的右键单击并调用一个函数来处理该事件监听器的标志赋值。 Can I also within a function disable the contextMenu from showing? 我是否也可以在函数内禁用contextMenu显示?

Thank you for any help you can offer! 感谢您提供的任何帮助!

The div being rendered looks like this right now: 正在渲染的div现在看起来像这样:

<div
   contextMenu="none"
   className="square"
   onContextMenu={() => this.props.rightClick(props.arrayVal)}
   onClick={() => {this.props.playerTurn(this.props.index)}}
   style={{color:COLORS[this.props.arrayVal], backgroundImage:this.backgroundChooser(this.props.arrayVal)}}
    >
       {this.squareContent(this.props.arrayVal)}
</div> 

The function being called looks like this: 被调用的函数如下所示:

rightClick = (id) => {
    let { board, gameWon, minesLeft } = this.state
    let mineCount = minesLeft
    let move = board
    if (gameWon === false) {
        if (board[id] === -1) {
            move[id] = 9
            mineCount -= 1
        } else if (board[id] === 9) {
            move[id] = "?"
            mineCount += 1
        } else if (board[id] === "?") {
            move[id] = -1
        }
        this.setState({
            board: move,
            minesLeft: mineCount
        })
    }
}

I was able to waste another long bit of time and get a senior dev friend to look at this for me. 我能够浪费另一段时间并让一位资深的开发朋友为我看这个。 Here was the solution. 这是解决方案。 Hope it helps someone else :-)! 希望它可以帮助别人:-)! See comment below: 见下面的评论:

I know its an old one but heres my solution for this: first, select the element you want to disable the contextMenu for (in this occasion its a div element) and just add this piece to the element: 我知道它是一个旧的但是我的解决方案:首先,选择你想要禁用contextMenu的元素(在这种情况下它是一个div元素),然后将这个片段添加到元素:

<div onContextMenu={(e)=> e.preventDefault()}... />

right clicking the div above won't trigger the context menu 右键单击上面的div将不会触发上下文菜单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM