[英]How to fix missing dependency in React applications using useEffect?
Getting this error in my console.在我的控制台中收到此错误。 Not sure how to handle them
不确定如何处理它们
I am passing in guildId, so im not sure why its logging this error我正在传递 guildId,所以我不确定为什么会记录此错误
src/utils/hooks/useFetchGuildBans.tsx
src/utils/hooks/useFetchGuildBans.tsx
Line 22:6: React Hook useEffect has a missing dependency: 'guildId'.
第 22:6 行:React Hook useEffect 缺少依赖项:“guildId”。 Either include it or remove the dependency array react-hooks/exhaustive-deps
包括它或删除依赖数组 react-hooks/exhaustive-deps
Here is my useFetchGuildBans.tsx这是我的 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]);
The second parameter of useEffect
is called the dependencies array. useEffect
的第二个参数称为 dependencies 数组。 The error message is saying that guildId
is missing from this array.错误消息是说此数组中缺少
guildId
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.