如何使用 useEffect 修复 React 应用程序中缺少的依赖项?

How to fix missing dependency in React applications using useEffect?

在我的控制台中收到此错误。不确定如何处理它们

我正在传递 guildId,所以我不确定为什么会记录此错误

src/utils/hooks/useFetchGuildBans.tsx

Line 22:6: React Hook useEffect has a missing dependency: 'guildId'. Either include it or remove the dependency array react-hooks/exhaustive-deps

这是我的useFetchGuildBans.tsx

import { useEffect, useState } from 'react';
import { getGuildBans } from '../api';
import { GuildBanType } from '../types';

export function useFetchGuildBans(guildId: string) {
  const [bans, setBans] = useState<GuildBanType[]>([]);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState();
  const [updating, setUpdating] = useState(false);

  useEffect(() => {
    setLoading(true);
    getGuildBans(guildId)
      .then(({ data }) => {
        setBans(data);
      })
      .catch((err) => {
        console.log(err);
        setError(err);
      })
      .finally(() => setLoading(false));
  }, [updating]);

  return { bans, loading, error, updating, setUpdating };
}
  useEffect(() => {
    setLoading(true);
    getGuildBans(guildId)
      .then(({ data }) => {
        setBans(data);
      })
      .catch((err) => {
        console.log(err);
        setError(err);
      })
      .finally(() => setLoading(false));
  }, [updating, guildId]);

useEffect的第二个参数叫做依赖数组。错误消息是说此数组中缺少 guildId