简体   繁体   English

onMouseOver 在数据变化时不断调用

[英]onMouseOver keeps calling when data changes

I have an application where I draw up to date locations on a map. These locations update about every second.我有一个应用程序,我在其中绘制了 map 上的最新位置。这些位置大约每秒更新一次。 I'm trying to have it so that I can hover over the locations, which are svg circles but onMouseOver keeps calling every time my useEffect updates the data and the locations on the map update.我正在尝试拥有它,以便我可以 hover 在位置上,这是 svg 个圆圈,但是每次我的 useEffect 更新数据和 map 更新上的位置时,onMouseOver 都会不断调用。 How can I fix this problem.我该如何解决这个问题。

So basically I have an svg, which has a list of the following elemnts in:所以基本上我有一个 svg,其中包含以下元素的列表:

const location = <circle
                     key = {'serialNumber'}
                     cx = {x}
                     cy = {y}
                     r = {4}
                     className="drone"
                     onMouseOver = {displayPilotName('name')}
                   />

And I draw that on a map. My useEffect works as following:我把它画在 map 上。我的 useEffect 的工作原理如下:

useEffect(() => {
  const interval = setInterval(() => {
  droneService.getAll().then(droneInfo =>
  setDrones(droneInfo))
  }, 500)
 return () => clearInterval(interval)
}, [])

I think the problem is because these two but might be cause by something else, but if I changed The interval to say 3 seconds it only called onMouseOver every 3 seconds.我认为问题是因为这两个但可能是由其他原因引起的,但是如果我将间隔更改为 3 秒,它只会每 3 秒调用一次 onMouseOver。 Also hover in css works fine, but I need to call an function from the hover so it doesn't work for this. css 中的 hover 工作正常,但我需要从 hover 调用 function,因此它不适用于此。

You are calling that function every render and passing whatever it returns... you should instead pass callback function您在每次渲染时都调用 function 并传递它返回的任何内容...您应该传递回调 function

const location = <circle
                     key = {'serialNumber'}
                     cx = {x}
                     cy = {y}
                     r = {4}
                     className="drone"
                     onMouseOver = {()=>displayPilotName('name')}
                   />

this will be triggered only on mouseOver这只会在 mouseOver 上触发

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

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