在React应用程序中将react-contextmenu与PIXI.js一起使用时,我遇到一个奇怪的错误(没有其他库在运行)。我有一个可以拖放到适当位置的地图组件。我发现,如果用户按住左键单击并左右摇动鼠标一会儿,即使用户未单击右键,它也会错误地触发上下文菜单的出现。它并没有破坏应用程序,但仍然是一个令人讨厌的错误,我想弄清楚如何消除它。相关代码如下:
import React from 'react'
import * as PIXI from 'pixi.js'
import {ContextMenu, MenuItem, ContextMenuTrigger} from 'react-contextmenu'
export default class MyComponent extends React.component{
constructor(props){
super(props)
}}
render(){
return <React.Fragment>
<ContextMenuTrigger id='aUniqueID'>
<div className='stageDiv'
ref=>{(el)=>{this.stageRef = el}}
/>
</ContextMenuTrigger>
<ContextMenu id='aUniqueID'>
<MenuItem onClick={this.myAction.bind(this)}/>
</ContextMenu>
</ReactFragment>
componentDidMount(){
const app = new PIXI.Application({
width:screen.width,
height:screen.height*0.8,
sharedLoader:true,
sharedTicker:true
})
app.renderer.plugins.interaction.on('mouseMove',this.mover.bind(this)).on('mousedown',this.onClick.bind(this))
this.stageRef.append(app.view)
app.stage.interactive = true
this.app = app
this.createMap()
this.setup()
{
this.createMap(){
//Just draws three sprites at equal intervals along the screen. No click events are bound to this code.
}
this.onClick(e){
this.setState({clickX:e.data.global.x,clickY:e.data.global.y,clicked:true})
}
onMouseMove(e){
if(this.state.clicked){
let x = e.data.global.x
let y = e.data.global.y
let xDiff = this.state.clickX - x
let yDiff = this.state.clickY - y
this.arrayOfDraggableSprites.forEach((sprite)=>{
sprite.x -= xDiff
sprite.y -= yDiff
}
}
我在该代码中没有看到我认为会触发错误的右键单击的任何内容,但希望其他人会这样做。
最佳答案
Figured it out。
默认情况下,react-contextmenu配置为可与移动设备一起使用,并且具有“保持显示”值1000毫秒。它假定用户在滚动时不会按住鼠标按钮1秒钟,而我的应用程序并非如此。
通过将显示的保留值设置为-1,它将不再触发错误。与往常一样,解决方案是寻求帮助,然后在一个小时后立即解决。
关于javascript - React-contextmenu收到一个错误的右键单击触发,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59931928/