React Hook Useffect缺少依赖性:' id'。包括它或删除依赖关系数组
对不起(也许)这个琐碎的问题,但我已经撞了几个小时了。 错误在哪里?
import axios from 'axios';
import React, { useEffect, useState } from 'react'
import { SingleCoin } from '../config/api';
const {id} = useParams();
const [coins, setCoins] = useState();
const {currency, symbol} = CryptoState();
useEffect(()=>{
const fetchCoins = async () => {
const {data} = await axios.get(SingleCoin(id));
setCoins(data);
};
fetchCoins();
}, []);
React Hook Useffect的依赖性缺失:“ ID”。包括它或删除依赖项数组
sorry for the trivial question (maybe) but I've been hitting my head for hours.
where is the mistake?
import axios from 'axios';
import React, { useEffect, useState } from 'react'
import { SingleCoin } from '../config/api';
const {id} = useParams();
const [coins, setCoins] = useState();
const {currency, symbol} = CryptoState();
useEffect(()=>{
const fetchCoins = async () => {
const {data} = await axios.get(SingleCoin(id));
setCoins(data);
};
fetchCoins();
}, []);
React Hook useEffect has a missing dependency: 'id'. Either include it or remove the dependency array
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在
usefect
函数中使用的任何变量都需要在依赖项数组中,以便可以对其进行监控,并且仅在变量更改时才能运行使用效应。将
id
添加到useffect
的第二个参数以清除此错误。参考
Any variable that you use in the
useEffect
function needs to be in the dependency array, so that it is can be monitored and the useEffect will only be run when that variable changes.Add
id
to the array that is the second parameter ofuseEffect
to clear this error.Reference
使用效果和其他一些钩子需要提供一个依赖关系阵列。这是作为数组的最后一个论点。依赖项告诉挂钩,要观察哪些变量或元素。如果依赖关系发生了变化,则钩子还应期望新的行为并因此更新。
要解决您的问题,您需要在依赖阵列中提供ID作为警告状态这样:
React Hook使用效应缺少依赖性:“ ID”
useEffect and some other hooks need a dependency array provided. It's the last argument passed as an array. The dependencies tell the hooks which variables or elements to observe for changes. If a dependency changes, the hook should also expect a new behavior and will therefor update.
To fix your issue, you need to provide the id in your dependency array as the warning states like so:
React Hook useEffect has a missing dependency: 'id'