reactjs - 尝试部署 React 应用程序时出现 react-hooks/exhaustive-deps 错误
问题描述
我正在尝试部署一个反应应用程序。该代码/应用程序在我的本地端运行良好。但是,当我尝试部署它时,我收到了这个错误:React Hook useEffect has a missing dependency: 'endGame'. Either include it or remove the dependency array react-hooks/exhaustive-deps
这是与错误相关的代码:
useEffect(() => {
if (isTimeRunning && timeRemaining > 0) {
setTimeout(() => {
setTimeRemaining((time) => time - 1);
}, 1000);
} else if (timeRemaining === 0) {
// eslint-disable-next-line react-hooks/exhaustive-deps
return endGame();
}
}, [timeRemaining, isTimeRunning]);
我尝试了几件事,包括将 endGame() 函数添加到数组中(作为 useEffect 的第二个参数),但这样做会破坏我的应用程序。
要查看整个代码/项目,请访问这里的 repo:https
: //github.com/umbur/SpeedTypingGame 在将此问题标记为不相关或类似之前,请询问我更多详细信息,谢谢!
解决方案
将函数添加到依赖项时不应调用该函数,而应传递函数引用。
尝试这个:[timeRemaining, isTimeRunning, endGame]
推荐阅读
- shell - mailx 不工作,但 sendmail 工作
- laravel - '忘记'每个集合中的键
- c++ - 重载 + 和 - 运算符时出错
- batch-file - 自动化 FFmpeg/多核支持
- python - 循环日期
- kubernetes - K8s集成EFK,状态码406被抛出
- python - Pyinstaller:exe文件不运行;编码错误
- logging - 配置 Kafka Connect 分布式连接器日志 (connectDistributed.out)
- javascript - CMS 我可以定位我要删除的字段,但是当我单击编辑按钮时,它不再定位它
- html - 变换rotateZ后如何减小ul大小?