- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在发布和运行 React Hook 错误之前进行我的第一个 React 构建,如下所示:“React Hook useEffect 缺少依赖项:'colors' 和 'options'。要么包含它们,要么删除依赖项数组。
我的组件在最后一行显示错误。我做错了什么?
function MemoryGame({ options, setOptions, highScore, setHighScore }) {
const [game, setGame] = useState([]);
const [flippedCount, setFlippedCount] = useState(0);
const [flippedIndexes, setFlippedIndexes] = useState([]);
const colors = [
`url(${Background1})`,
`url(${Background13})`,
`url(${Background3})`,
`url(${Background4})`,
`url(${Background5})`,
`url(${Background6})`,
`url(${Background7})`,
`url(${Background8})`,
`url(${Background9})`,
`url(${Background14})`,
`url(${Background11})`,
`url(${Background12})`,
];
useEffect(() => {
const newGame = [];
for (let i = 0; i < options / 2; i++) {
const firstOption = {
id: 2 * i,
colorId: i,
color: colors[i],
flipped: false,
};
const secondOption = {
id: 2 * i + 1,
colorId: i,
color: colors[i],
flipped: false,
};
newGame.push(firstOption);
newGame.push(secondOption);
}
const shuffledGame = newGame.sort(() => Math.random() - 0.5);
setGame(shuffledGame);
}, []);
我刚刚添加了 [colors, options]
但现在我得到了这个: 'colors' 数组使 useEffect Hook 的依赖项(第 137 行)在每次渲染时都会发生变化。将它移到 useEffect 回调中。或者,将 'colors' 的初始化包装在它自己的 useMemo() Hook 中。
现在我尝试在 useEffect
中移动 const colors
并且变得未定义。我还做错了什么?
useEffect(() => {
const colors = [
`url(${Background1})`,
`url(${Background13})`,
`url(${Background3})`,
`url(${Background4})`,
`url(${Background5})`,
`url(${Background6})`,
`url(${Background7})`,
`url(${Background8})`,
`url(${Background9})`,
`url(${Background14})`,
`url(${Background11})`,
`url(${Background12})`,
];
const newGame = [];
for (let i = 0; i < options / 2; i++) {
const firstOption = {
id: 2 * i,
colorId: i,
color: colors[i],
flipped: false,
};
const secondOption = {
id: 2 * i + 1,
colorId: i,
color: colors[i],
flipped: false,
};
newGame.push(firstOption);
newGame.push(secondOption);
}
const shuffledGame = newGame.sort(() => Math.random() - 0.5);
setGame(shuffledGame);
}, [colors, options]);
最佳答案
不要忽视警告,它的存在是有充分理由的。它的目的是通知您,您正在使用对在钩子(Hook)本身之外定义的变量的引用,这些变量可能会发生变异并使您处于陈旧状态。
useEffect
钩子(Hook)接受 2 个参数,其中第二个是一个依赖项数组 - 在您的例子中是一个空的。基于此数组,React 知道何时应执行 useEffect
中的回调。在你的情况下提供一个空数组将导致回调只运行一次,在组件的初始渲染之后并且不会再运行,无论你的组件发生什么变化。因此,即使您更改了 colors
和 options
引用, Hook 也不会执行,您将处于陈旧状态,因此会出现警告。但是,您的代码实际上依赖于这两个引用,因此您应该将它们放在依赖数组中,以指示 react 在它们中的任何一个发生变化时重新运行回调函数(即使它们是常量并且您知道不会改变) .所以,使用这个:
useEffect(() => {
const newGame = [];
for (let i = 0; i < options / 2; i++) {
const firstOption = {
id: 2 * i,
colorId: i,
color: colors[i],
flipped: false,
};
const secondOption = {
id: 2 * i + 1,
colorId: i,
color: colors[i],
flipped: false,
};
newGame.push(firstOption);
newGame.push(secondOption);
}
const shuffledGame = newGame.sort(() => Math.random() - 0.5);
setGame(shuffledGame);
}, [colors, options]);
基本上,如果一个变量(包括函数)只在 useEffect
回调中使用,你应该在里面定义它,你可以跳过将它添加到依赖数组,因为它可以在内部跟踪它。在极少数情况下,当您仅依赖于 useEffect
的变量的初始状态(可以在之后发生变异)并且您只想在组件“setup”上运行一次 Hook 时,您应该跳过添加它到依赖数组,它不会导致它重新执行。
关于reactjs - React Hook useEffect 缺少依赖项 : 'colors' and 'options' . 包括它们或删除依赖项数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66017049/
我正在尝试在 map 上绘制一些疾病事件数据的位置。 我用它来导入数据: ByTown% addProviderTiles("CartoDB.Positron")%>% addPolygons
我有一个文件调用 find.js,我使用 node find.js 运行,我的节点是版本 10 我不知道为什么我无法使用 async await。 const axios = require("axi
我有一个项目作为引用添加到 System.Web。 但是,它似乎无法获取 HttpContext。这样做: Imports System.Web _ApplicationBase = HttpCont
在互联网上找到这段代码,出于某种原因它缺少 while 循环逻辑“while(i....)”,虽然我找到了 PigLatin* 问题的其他可行解决方案,但我真的很想了解这个正在工作。 *PigLati
我工作了一整天来运行 Xampp 并在其上安装 TYPO3。现在我登录到后端,但没有显示许多管理模块,例如模板、访问等。 - 一定是我做错了什么,但我不知道。 these are the module
你好 我有编译这个问题 \begin{equation} J = \sum_{j=1}^{C} \end{equation} 我不断收到错误 missing $ inserted 这很奇怪,因
我正在尝试使用 SQLite CLI,但无法获得 generate_series功能来工作。我可以按照文档中的建议使用递归 CTE 对其进行模拟,但我似乎无法获得该链接中的任何示例。这是我的 sess
我目前正在开发我想要的软件,而软件正在安装,它可以在后台为软件创建 native 图像。 我正在考虑使用 NGEN 并将进程优先级设置为低,因为我不希望它消耗 100% CPU。但是我发现我的计算机上
我想使用 Xcodes Instruments 进行 UI 自动化测试。但似乎缺少“自动化”。我怎样才能添加这个? 最佳答案 如果您想使用自动化仪器,请使用 Xcode 7.3。 Apple 在 Xc
我目前在 JS 开发中迈出了一小步,并编写了以下链接添加器: const button = document.getElementById('button') const listdiv = docu
此代码有什么问题: NSError *error = nil; [SFHFKeychainUtils deleteItemForUsername:@"IAPNoob01" andServiceName
出于某种原因,在安装和配置(我认为)一切之后,com.adobe.utils.AGALMiniAssembler 不见了,其他一切正常。 我认为我已尽一切努力让孵化器正常工作,但显然我错过了一步。 如
我有一个名为 new 的方法。调用 new 时,我传递了一个参数,但是当我运行应用程序时,出现没有参数或参数为空的错误。 StepReader.pm package StepReader; use s
安装 gtk 1.2(包名 gtk1)和 macports chokes 在最终的 make 中,在 libintl.h 的第 440 行。 extern locale_t libintl_newlo
我用按钮创建表格。 这是javascript代码: function layersListTable(layers) { var content =''; $.each($(layer
我在使用此 javascript 时遇到此错误,任何人都可以帮我弄清楚我做错了什么吗? $(this).prepend('Check availability »'); 它给我错误 mis
我有一个独立的工具链 NDK13b、api19、llvm 3.8 编译器、arm 32 位、带有 libcpp(llvm C++ 库) 我想避免依赖 libgcc,所以我构建了 compiler-rt
我按照一些教程使用 phonegap 的条形码扫描器插件。但是当我从现有源创建一个新的 android 项目来创建条码库时 (step 6 in this page)我收到错误:“AndroidMan
我现在尝试在 Eclipse 中打开我的布局 xml 文件。我只得到错误 No XML content. Please add a root view or layout to your docume
我的 android-sdk-windows\tools 目录中缺少层次结构查看器工具。 工具链接: http://developer.android.com/guide/developing/too
我是一名优秀的程序员,十分优秀!