reactjs - 尝试实现倒计时时反应过多的重新渲染
问题描述
如果您能帮助我,我将不胜感激。所以基本上我试图在反应应用程序上显示倒计时。但是,我收到了太多渲染发生的错误。我相信这是因为我更新了 useEffect 钩子本身的计数器,但我在网上看到了做同样事情的例子(据他们说,它有效)
const [question, setQuestion] = useState([]);
const [counter, setCounter] = useState(60);
if (question === []) {
//make API request
setQuestion(apiResponse) //confirmed this works
} else {
//endingTime and currentTime are variables with the correct values (confirmed this)
setCounter(endingTime - currentTime);
}
React.useEffect(() => {
const timer = counter > 0 && setInterval(() => setCounter(counter - 1), 1000);
if (counter === 0) setCounter('Time is Up');
return () => clearInterval(timer);
}, [counter]);
解决方案
推荐阅读
- delphi - SaveState 异常:无法创建文件,不允许操作
- python - 将边界框中的pdf文本直接提取到python中
- javascript - 使用 JS 创建两个新元素
- android - ACTION_OPEN_DOCUMENT_TREE 在 Android Q Beta 2 模拟器中不起作用?
- c# - 为什么我的存储过程没有在 DBScaffold (.Net Core MVC) 之后列出
- python - 如何修改 For 循环中的当前元素?
- excel - 将临时文件中的图像嵌入电子邮件时如何使用属性访问器
- ios - 快速检测 Viewcontroller 上的第一次触摸
- php - 在 PHP 中使用 cURL 解析错误发布数据
- kubernetes - 未完成的节点导出器 pod 的 Prometheus 错误在哪里?