- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
不知怎么的,我遇到了一个无限循环,我确实解决了这个奇怪的情况,但我收到了一个警告,我希望修复这个警告。
这是有效的代码:
import { ArrowDropDown, ArrowRight } from "@material-ui/icons";
import React, { useState, useEffect, useCallback } from "react";
import "./tree.css";
const Tree = ({ explorer }) => {
const [expand, setExpand] = useState(true);
const [arrow, setArrow] = useState(false);
const stateHandler = () => {
setExpand(!expand);
setArrow(!arrow);
};
useEffect(() => {
//this function will display only the first Tree as init the page.
stateHandler();
}, []);
return (
<div>
<div className="treeInfo">
{arrow ? (
<ArrowDropDown className="treeIcon" />
) : (
<ArrowRight className="treeIcon" />
)}
<span className="treeTitle" onClick={stateHandler}>
{explorer.name}
</span>
</div>
<div
style={{
display: expand ? "block" : "none",
paddingLeft: 20,
cursor: "pointer",
}}
>
{explorer.items.map((explore) => {
return <Tree key={explore.id} explorer={explore} />;
})}
{/* {explorer.items.map((explore) => (
<Tree explorer={explore} />
))} */}
</div>
</div>
);
};
export default Tree;
这是警告:
src\components\Tree\Tree.js
Line 17:6: React Hook useEffect has a missing dependency: 'stateHandler'. Either include it or remove the dependency array react-hooks/exhaustive-deps
Search for the keywords to learn more about each warning.
To ignore, add // eslint-disable-next-line to the line before.
WARNING in src\components\Tree\Tree.js
Line 17:6: React Hook useEffect has a missing dependency: 'stateHandler'. Either include it or remove the dependency array react-hooks/exhaustive-deps
webpack compiled with 1 warning
**
**
据我所知,我收到此警告是因为 useEffect 没有依赖项,当我添加 stateHandler 依赖项时,我得到了一个无限循环,所以我添加了一个回调函数,但它仍然没有解决无限循环。这是带有 useCallback 的代码(它是相同的代码,只是带有 useCallback 和一些 useEffect 的配置):
const initTree = useCallback(() => {
setExpand(!expand);
setArrow(!arrow);
}, [setExpand, setArrow, arrow, expand]);
useEffect(() => {
//this function will display only the first Tree as init the page.
initTree();
}, [initTree]);
最佳答案
你需要改变这个:
const initTree = useCallback(() => {
setExpand(!expand);
setArrow(!arrow);
}, [setExpand, setArrow, arrow, expand]);
为此:
const initTree = useCallback(() => {
setExpand(e => !e);
setArrow(a => !a);
}, []);
否则会发生这样的事情:
initTree
变量用函数初始化,Effect
运行,setExpand
和 setArrow
被调用。useCallback
Hook 检查 initTree
的 deps 是否已更改,是的,arrow
和 expand
已更改事实上,因此 initTree
变量被更新为一个新函数initTree
是否与之前的渲染发生了变化,是的,它已经发生变化,因此效果会再次调用 initTree
执行。如果您不将 setState
放在 deps 中,Eslint 不应该提示缺少 deps,因为它们在渲染期间不会改变,除非您通过 props 传递它们。
关于javascript - 使用 useEffect 和 useCallback 无限循环 - Reactjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72147191/
我正在优化应用程序的性能,我想知道是否对那些不依赖任何变量的函数使用 useCallback 钩子(Hook)。 考虑以下情况: 假设我们有一些功能: const someFunc = () => {
在我尝试更好地理解 React Hooks 的过程中,我遇到了一些我没有预料到的行为。我试图创建一个引用数组,并通过 onRef 函数推送到所述数组,我将传递给我的 's 。每次组件重新渲染时,数组都
我对 useMemo 还很陌生和 useCallback react 中的钩子(Hook),我有点不明白为什么即使我检查的变量保持不变,我的函数仍然继续重新渲染。目前我正在尝试构建一个谷歌地图,允许用
最近在研究react-hook。我在实践的过程中遇到了一个让我无法理解的问题。 import React, { useState, useCallback } from 'react'; const
根据 React 官方文档,如果组件新状态是使用先前状态计算的,则可以将函数传递给 setState。该函数将接收先前的值,并返回更新后的值。 考虑以下代码片段示例: const [counter,
为什么useCallback 钩子(Hook)会执行两次?我收到一条警告,建议我使用 useCallback,所以我正在尝试这样做。据我了解,useCallback 只会在我们传递给数组的对象更新时执
目前,每次更新查询参数时,我的代码都会重新呈现。一旦我删除了查询参数;但是,我收到一条警告,指出“React Hook useCallback 缺少依赖项:'query'。要么包含它,要么删除依赖项数
使用 React 的 useCallback hook 本质上只是 useMemo 的包装。专门用于函数,以避免在组件的 props 中不断创建新的函数实例。我的问题来自于当您需要将争论传递给从内存创
React 文档中关于 useMemo 的内容如下 You may rely on useMemo as a performance optimization, not as asemantic gu
在我读过的 React 文档 Pass an inline callback and an array of dependencies. useCallback will return a memoi
在我读过的 React 文档 Pass an inline callback and an array of dependencies. useCallback will return a memoi
什么时候应该使用 useCallback 什么时候应该使用简单的数组函数?或者也许我不应该使用第二种方法? const MyCompenent = (props) => { const handl
使用 React Hooks,当我们想要记住函数的创建时,我们有 useCallback 钩子(Hook)。这样我们就有了: const MyComponent = ({ dependancies }
我有接受函数作为参数的钩子(Hook)。如果功能发生不必要的更改,则可能会导致额外的重新渲染。我想确保函数用 useCallback 包装。 即这是一个简化版本: function useApi({
考虑这个带有自定义表单钩子(Hook)的基本表单字段组件来处理输入更改: import React, { useState, useCallback } from 'react'; const use
我正在检测组件中的按键,该组件知道当前通过 Prop 在应用程序的其他地方打开了什么对话框组件, currentDialog .通常我可以在嵌套函数中访问这个 Prop ,但在使用 useCallba
我正在学习 Advanced React Patterns with Hooks 在线类(class),在这个早期示例中,他们使用以下 API 创建了一个可扩展组件(比如经典的 Accordion 或
我是 React 的新手,我收到了这个错误: Warning: Can't perform a React state update on an unmounted component. This i
我的回调在一次又一次调用后返回相同的状态 我是 react 类中的 react 钩子(Hook)的新手,我本可以使用 shouldcomponentupdate 并且可以解决这个问题 但是 useca
下面的示例是一个简化的摘录,其中子组件根据鼠标行为发出事件。然后 React 应根据发出的事件更新 DOM。 function SimpleSample() { const [addons, se
我是一名优秀的程序员,十分优秀!