React Hook Useffect缺少依赖性:' id'。包括它或删除依赖关系数组

发布于 2025-02-12 10:58:54 字数 538 浏览 2 评论 0原文

对不起(也许)这个琐碎的问题,但我已经撞了几个小时了。 错误在哪里?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑶笙 2025-02-19 10:58:54

您在usefect函数中使用的任何变量都需要在依赖项数组中,以便可以对其进行监控,并且仅在变量更改时才能运行使用效应。

useEffect(()=>{
    const fetchCoins = async () => {
        const {data} = await axios.get(SingleCoin(id));
        setCoins(data);
    };
    fetchCoins();
}, [id]);

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.

useEffect(()=>{
    const fetchCoins = async () => {
        const {data} = await axios.get(SingleCoin(id));
        setCoins(data);
    };
    fetchCoins();
}, [id]);

Add id to the array that is the second parameter of useEffect to clear this error.
Reference

硬不硬你别怂 2025-02-19 10:58:54

使用效果和其他一些钩子需要提供一个依赖关系阵列。这是作为数组的最后一个论点。依赖项告诉挂钩,要观察哪些变量或元素。如果依赖关系发生了变化,则钩子还应期望新的行为并因此更新。

要解决您的问题,您需要在依赖阵列中提供ID作为警告状态这样:
React Hook使用效应缺少依赖性:“ ID”

useEffect(()=>{
    const fetchCoins = async () => {
        const {data} = await axios.get(SingleCoin(id));
        setCoins(data);
    };
    fetchCoins();
}, [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'

useEffect(()=>{
    const fetchCoins = async () => {
        const {data} = await axios.get(SingleCoin(id));
        setCoins(data);
    };
    fetchCoins();
}, [id]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文