简体   繁体   English

React Hook useEffect 缺少依赖项:'fetchTracker'。 包括它或删除依赖项数组

[英]React Hook useEffect has a missing dependency: 'fetchTracker'. Either include it or remove the dependency array

I have come across this issue in one of my projects.我在我的一个项目中遇到过这个问题。 This specific file tracks data so that is can be placed on a map in an application.该特定文件跟踪数据,以便将其放置在应用程序中的地图上。

I have looked elsewhere on StackOverflow for an explanation and resolution for the issue, but I haven't found anything that works.我在 StackOverflow 上的其他地方查看了该问题的解释和解决方案,但我没有找到任何有效的方法。 Any insight would be appreciated.任何见解将不胜感激。

import { useEffect, useState } from 'react';
import axios from 'axios';

const API_HOST = '...';
const ENDPOINTS = [...];
const defaultState = {...};

const useTracker = ({ api = 'all' }) => {
    const [tracker = {}, updateTracker] = useState(defaultState);

    async function fetchTracker() {
        let route = ENDPOINTS.find(({ id } = {}) => id === api);
        let response;

        if (!route) {...}

        try {...} catch (e) {...}

        const { data } = response;

        updateTracker((prev) => {
            return {
                ...prev,
                state: 'ready',
                data
            };
        });
    }

    useEffect(() => {
        fetchTracker();
    }, [api]);

    return {
        fetchTracker,
        ...tracker
    };
};

export default useTracker;

Thanks谢谢

You need to include it to prevent side effect.您需要包含它以防止副作用。 To prevent a infinity loop wrap it in useCalback:为了防止无限循环将其包装在 useCalback 中:


const useTracker = ({ api = 'all' }) => {
    const [tracker = {}, updateTracker] = useState(defaultState);

    const fetchTracker = React.useCallback(async () => {
        let route = ENDPOINTS.find(({ id } = {}) => id === api);
        let response;

        if (!route) {...}

        try {...} catch (e) {...}

        const { data } = response;

        updateTracker((prev) => {
            return {
                ...prev,
                state: 'ready',
                data
            };
        });
    },[api]);

    useEffect(() => {
        fetchTracker();
    }, [api, fetchTracker]);

    return {
        fetchTracker,
        ...tracker
    };
};

暂无
暂无

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

相关问题 React Hook useEffect 缺少依赖项:'formData'。 包括它或删除依赖项数组。 什么是依赖就是使用 - React Hook useEffect has a missing dependency: 'formData'. Either include it or remove the dependency array. what is dependency is use React Hook useEffect 缺少一个依赖项:'handleLogout'。 要么包含它,要么移除依赖数组 react - React Hook useEffect has a missing dependency: 'handleLogout'. Either include it or remove the dependency array react React Hook useEffect 缺少依赖项:'id'。 包括它或删除依赖数组 - React Hook useEffect has a missing dependency: 'id'. Either include it or remove the dependency array React Hook useEffect 缺少一个依赖项:'refreshSells'。 包括它或删除依赖数组 - React Hook useEffect has a missing dependency: 'refreshSells'. Either include it or remove the dependency array React Hook useEffect 缺少依赖项:“match.params.roomid”。 包含它或删除依赖项数组 - React Hook useEffect has a missing dependency: 'match.params.roomid'. Either include it or remove the dependency array React Hook useEffect 缺少依赖项:'formValues'。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'formValues'. Either include it or remove the dependency array react-hooks/exhaustive-deps 错误:React Hook useEffect 缺少依赖项:“setAPIData”。 包含它或删除依赖项 array.eslintreact-hooks/exhaustive-deps - Error: React Hook useEffect has a missing dependency: 'setAPIData'. Either include it or remove the dependency array.eslintreact-hooks/exhaustive-deps 警告:React Hook useEffect 缺少依赖项:'postImage'。 在 npm 运行构建时包含它或删除依赖数组 - Warning: React Hook useEffect has a missing dependency: 'postImage'. Either include it or remove the dependency array while npm run build React Hook useEffect 缺少依赖项。 包括它们或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has missing dependencies. Either include them or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:“roomID”和“sotreId”。 要么包含它们,要么删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has missing dependencies: 'roomID 'and 'sotreId'. Either include them or remove the dependency array react-hooks/exhaustive-deps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM