首页 > 解决方案 > 尝试部署 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 在将此问题标记为不相关或类似之前,请询问我更多详细信息,谢谢!

标签: reactjsdeploymentreact-hooksuse-effectreact-functional-component

解决方案


将函数添加到依赖项时不应调用该函数,而应传递函数引用。

尝试这个:[timeRemaining, isTimeRunning, endGame]


推荐阅读