简体   繁体   English

React Hook useEffect 缺少依赖项。 要么包含它,要么移除依赖数组 react-hooks/exhaustive-deps

[英]React Hook useEffect has a missing dependency. Either include it or remove the dependency array react-hooks/exhaustive-deps

If i add the dependency array "fitems" in the dependecy array like its telling me to do, then it causes infinite loop.如果我像它告诉我的那样在依赖数组中添加依赖数组“fitems”,那么它会导致无限循环。 also if i dont use the spread operator on the array then the warning doesnt show but then the state change doesnt rerender.另外,如果我不在数组上使用扩展运算符,则不会显示警告,但 state 更改不会重新呈现。

Sidebar.tsx侧边栏.tsx

import { useState, useEffect } from "react";
import { Link, useLocation } from "react-router-dom";
import axios from "axios";
import getItems from "./../services/SidebarItems";
import { sidebarInfoUrl } from "./../services/ApiLinks";

function Sidebar() {
  const fItems = getItems();
  const location = useLocation();
  const paths = location.pathname.split("/");
  const [items, setItems] = useState(fItems);

  useEffect(() => {
    axios.get(sidebarInfoUrl).then((response) => {
      const updatedItems = [...fItems]
      updatedItems.forEach((item) => {
        if (item.match === "projects") item.value = response.data.projects;
        else if (item.match === "contacts") item.value = response.data.contacts;
      });
      setItems(updatedItems);
      console.log("here")
    });
  }, []);

  return (
    <div className="sidebar shadow">
      {items &&
        items.map((item) => (
          <Link
            key={item.match}
            to={item.link}
            className={
              paths[2] === item.match ? "sidebar-item active" : "sidebar-item"
            }
          >
            <span>
              <i className={item.icon}></i> {item.title}
            </span>
            {item.value && <div className="pill">{item.value}</div>}
          </Link>
        ))}
    </div>
  );
}

export default Sidebar;

Here is the sidebar items i am getting from getItems().这是我从 getItems() 获得的侧边栏项目。

sidebarItems.ts sidebarItems.ts

const items = () => {
  return [
    {
      title: "Dashboard",
      icon: "fas fa-home",
      link: "/admin/dashboard",
      match: "dashboard",
      value: "",
    },
    {
      title: "Main Page",
      icon: "fas fa-star",
      link: "/admin/main-page",
      match: "main-page",
      value: "",
    },
    {
      title: "Projects",
      icon: "fab fa-product-hunt",
      link: "/admin/projects",
      match: "projects",
      value: "00",
    },
    {
      title: "Contacts",
      icon: "fas fa-envelope",
      link: "/admin/contacts",
      match: "contacts",
      value: "00",
    },
  ];
};

export default items;

Thank to AKX.感谢AKX。 I found my problem.我发现了我的问题。 I had to use useMemo Hook so that my getItem() function doesnt cause infinte loop when i add it to dependency array.我必须使用useMemo Hook 以便当我将 getItem() function 添加到依赖数组时不会导致无限循环。

const fItems = useMemo(() => {
    return getItems();
  }, []);

instead of代替

const fItems = getItems();

Another fix is that,另一个解决方法是,
If i dont send the items from SidebarItems.ts as function but as an array then it wont cause the infinte loop even if i dont use useMemo hook.如果我不将 SidebarItems.ts 中的项目作为 function 但作为数组发送,那么即使我不使用useMemo钩子,它也不会导致无限循环。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Hook useCallback 缺少依赖项。 要么包含它,要么移除依赖数组 react-hooks/exhaustive-deps - React Hook useCallback has a missing dependency. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项要么包含它,要么删除依赖项数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:&#39;user.id&#39;。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'user.id'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:“url”。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'url'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:&#39;dispatch&#39;。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:&#39;fetchProfile&#39;。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'fetchProfile'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:&#39;formValues&#39;。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'formValues'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:包含它或删除依赖项数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: Either include it or remove the dependency array react-hooks/exhaustive-deps 第 93:6 行:React Hook useEffect 缺少依赖项:'estado'。 要么包含它,要么移除依赖数组 react-hooks/exhaustive-deps - Line 93:6: React Hook useEffect has a missing dependency: 'estado'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:'loading'。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'loading'. Either include it or remove the dependency array react-hooks/exhaustive-deps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM