简体   繁体   English

React Hook useEffect 缺少依赖项警告

[英]React Hook useEffect has a missing dependency warning

In a React app I get this warning on a few components in the useEffect function.在 React 应用程序中,我在 useEffect function 中的一些组件上收到此警告。 I have seen other SO questions but still cant see a fix.我已经看到了其他 SO 问题,但仍然看不到修复。

React Hook useEffect has a missing dependency: 'loadItems'. React Hook useEffect 缺少一个依赖项:'loadItems'。 Either include it or remove the dependency array包括它或删除依赖数组

import React, { useState, useEffect } from "react";

import Button from "./common/button";
import { splitArray } from "../utility/chunkify";

const BudgetTypesList = ({ types, onDelete, onEdit }) => {
  const [items, setItems] = useState([]);

  useEffect(() => {
    loadItems();
  }, [types]);

  const loadItems = () => {
    const size = Math.ceil(types.length / 2);
    const chunks = splitArray(types, size);
    setItems(chunks);
  };

   .... rest of code here

UseEffect suggests to declare all the function/values as a dependency. UseEffect建议将所有function/values声明为依赖项。

import React, { useState, useEffect } from "react";

import Button from "./common/button";
import { splitArray } from "../utility/chunkify";

const BudgetTypesList = ({ types, onDelete, onEdit }) => {
  const [items, setItems] = useState([]);

  useEffect(() => {
    const loadItems = () => { //<---move this function inside useEffect
      const size = Math.ceil(types.length / 2);
      const chunks = splitArray(types, size);
      setItems(chunks);
    };
    loadItems();
  // you can use this to `disable comment` any such unnecessary warnings
  // eslint-disable-next-line 
  }, [types]); //<--- It will also show it for setItems now,

  

   .... rest of code here

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

相关问题 React useEffect 钩子缺少依赖项警告 - React useEffect hook has a missing dependency warning 警告:React Hook useEffect 缺少依赖项 - warning: React Hook useEffect has a missing dependency 当 deps 为 [] 时,React 警告 React Hook useEffect 缺少依赖项 - React warning React Hook useEffect has a missing dependency when the deps are [] &#39;React Hook useEffect has an missing dependency&#39; 警告功能 - 'React Hook useEffect has a missing dependency' warning with function 我的应用程序的“React Hook useEffect 缺少依赖项”警告 - My App's "React Hook useEffect has a missing dependency" warning 如何修复这个“React Hook useEffect has a missing dependency”警告? - How to fix this “React Hook useEffect has a missing dependency” warning? 如何解决此警告:“React Hook useEffect 缺少依赖项:'history'”? - How to fix this warning: “React Hook useEffect has a missing dependency: 'history'”? React Hook useEffect 缺少依赖项:'setValid'。 如何删除此警告 - React Hook useEffect has a missing dependency: 'setValid' . How to remove this warning React Hook useEffect 缺少对 useEffect 的依赖 - React Hook useEffect has a missing dependency with useEffect UseEffect - React Hook useEffect 缺少依赖项: - UseEffect - React Hook useEffect has a missing dependency:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM