简体   繁体   English

React JS onClick() 有时不起作用

[英]React JS onClick() Sometimes doesn't work

I have been trying to fix this for hours.我已经尝试解决这个问题好几个小时了。 I found this issue originally on iOS for a website I had released.我最初在 iOS 上为我发布的一个网站发现了这个问题。 I have tested with the following code, and it will work 90% of the time, but randomly will not work.我已经用下面的代码进行了测试,90% 的时间它都可以工作,但随机的就不行了。

<button className="p-8" onClick={() => (console.log("successful click"))}>test</button>

I use tailwindcss, but have tried adding the cursor: "pointer" style to the button and parent divs, which changed nothing, I have tried adding an id with a separate javascript addEventHandler, which changed nothing, I have tried adding onClick="void(0)" to both parent and child divs, which did nothing, I tried calling onTouchStart alongside onClick, which somewhat solved this issue but cause another issue where things were being double-clicked.我使用tailwindcss,但尝试将光标:“指针”样式添加到按钮和父div,这没有改变,我尝试使用单独的javascript addEventHandler添加一个id,它没有改变,我尝试添加onClick =“void (0)" 到父 div 和子 div,它什么也没做,我尝试在 onClick 旁边调用 onTouchStart,这在一定程度上解决了这个问题,但导致了另一个问题,即双击。 I have tried EVERYTHING that I could find on the internet and nothing is working.我已经尝试了所有可以在互联网上找到的东西,但没有任何效果。

I figured it out.我想到了。 Upon inspecting the page, I saw that several of my buttons were flashing on the 'elements' panel.检查页面后,我看到我的几个按钮在“元素”面板上闪烁。 I had a separate function being called once per second that was updating the state of a number (I have a countdown timer on my page) that was completely unrelated to this button.我有一个单独的函数每秒调用一次,它正在更新与此按钮完全无关的数字状态(我的页面上有一个倒数计时器)。

Updating the state of that item was causing every button to re-render every second, which takes milliseconds, but if you happened to click right when it rendered it would do nothing.更新该项目的状态会导致每个按钮每秒重新渲染一次,这需要几毫秒,但如果您在它渲染时碰巧单击了它,它将什么也不做。 Removing the logic of the timer setState() actually solved all of my problems with the clicking.删除计时器 setState() 的逻辑实际上解决了我所有的点击问题。

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

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