简体   繁体   English

如何在自定义挂钩中修复“useEffect 缺少依赖项”

[英]How do I fix “useEffect has a missing dependency” in custom hook

I'm using a custom hook to get pull some data in from an API for use across a set of React function components.我正在使用自定义钩子从 API 中获取一些数据,以便在一组 React function 组件中使用。 However, esLint throws up a lovely warning:然而,esLint 抛出了一个可爱的警告:

React Hook useEffect has a missing dependency: 'fetchFromAPI'. React Hook useEffect 缺少依赖项:'fetchFromAPI'。 Either include it or remove the dependency array.包括它或删除依赖数组。

I didn't think it's a dependency, as it's inside useFetch() itself.我不认为这是一个依赖,因为它在useFetch()本身内部。 I need to do it as I'm using await .我需要这样做,因为我正在使用await What am I doing wrong?我究竟做错了什么? Is it ok to just turn off the warning for this line?关闭这条线路的警告可以吗? Or is there a more canonical syntax I should be using?还是我应该使用更规范的语法?

function useFetch (url) {
  const [data, setData] = useState(null);

  async function fetchFromAPI() {
    const json = await( await fetch(url) ).json();
    setData(json);
  }

  useEffect(() => {fetchFromAPI()},[url]);

  return data;
};

export {
  useFetch
};

Declare it outside your custom effect passing url as parameter and return the json to be setted inside useEffect在自定义效果之外声明它,传递url作为参数并return json以在useEffect中设置

async function fetchFromAPI(url) {
    const json = await( await fetch(url) ).json();
    return json
}

function useFetch (url) {
  const [data, setData] = useState(null);

  useEffect(() => {
      setData(fetchFromAPI(url))
  },[url]);

  return data;
};

Or directly inside useEffect或者直接在useEffect里面

function useFetch (url) {
  const [data, setData] = useState(null);

  useEffect(() => {
      async function fetchFromAPI() {
         const json = await( await fetch(url) ).json();
         return json
      }
      setData(fetchFromAPI())
  },[url]);

  return data;
};

I suggest you to define async function inside useEffect itself:我建议您在 useEffect 本身中定义 async function :

function useFetch (url) {
  const [data, setData] = useState(null);
  useEffect(() => {
    async function fetchFromAPI() {
      const json = await( await fetch(url) ).json();
      setData(json);
    }
    fetchFromAPI()
  },[url]);

  return data;
};

You can take a look at valid example from doc faqs which uses async function inside useEffect itself:您可以查看doc faqs 中的有效示例,该示例在 useEffect 本身中使用异步 function:

function ProductPage({ productId }) {
  const [product, setProduct] = useState(null);

  useEffect(() => {
    // By moving this function inside the effect, 
    // we can clearly see the values it uses.
    async function fetchProduct() {
      const response = await fetch('http://myapi/product' + productId);
      const json = await response.json();
      setProduct(json);
    }

    fetchProduct();
  }, [productId]); // ✅ Valid because our effect only uses productId
  // ...
}

Just move your function inside the useEffect, and everything will be fine:只需将您的 function 移动到 useEffect 中,一切都会好起来的:

import { useState, useEffect } from "react";

function useFetch(url) {
  const [data, setData] = useState(null);

  useEffect(() => {
    async function fetchFromAPI() {
      const json = await (await fetch(url)).json();
      setData(json);
    }
    fetchFromAPI();
  }, [url]);

  return data;
}

export { useFetch };

https://codesandbox.io/s/clever-cdn-8g0me https://codesandbox.io/s/clever-cdn-8g0me

async function fetchFromAPI(url) {
  return ( await fetch(url) ).json();
}

function useFetch (url) {
  const [data, setData] = useState(null);

  useEffect(() => {
    fetchFromAPI(url).then(setData);
  }, [url, setData, fetchFromAPI]);

  return data;
};

export {
  useFetch
};
  1. You can change a little and then extract fetchFromAPI , for not being created every time useFetch calls, also it's good for single responsibility.您可以稍作更改,然后提取fetchFromAPI ,因为每次调用 useFetch 时都不会创建,这对单一职责也有好处。
  2. If you understand this code very well of course you can either turn off linting for this current line or you can add the rest setData and fetchFromAPI params.如果您非常了解此代码,当然您可以关闭当前行的 linting,或者您可以添加 rest setDatafetchFromAPI参数。 And exactly in this order.并且完全按照这个顺序。 Because on re-render params comparison start from first param to last, and it's better place most changed param in first place, for not checking not changed param every time the next one is changed, so if first changed, useEffect don't need to check the others and call passed function earlier因为在重新渲染参数比较从第一个参数到最后一个参数时,最好将更改最多的参数放在第一位,因为每次更改下一个参数时都不检查未更改的参数,所以如果第一次更改, useEffect不需要检查其他人并提前致电通过 function

暂无
暂无

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

相关问题 修复 React Hook useEffect 缺少依赖项 - Fix React Hook useEffect has a missing dependency React Hook useEffect 缺少依赖项:'notes',如何修复? - React Hook useEffect has a missing dependency: 'notes', how to fix it? 如何修复 React Hook useEffect 缺少依赖项 - How to fix React Hook useEffect has a missing dependency 如何修复这个“React Hook useEffect has a missing dependency”警告? - How to fix this “React Hook useEffect has a missing dependency” warning? 如何解决此警告:“React Hook useEffect 缺少依赖项:'history'”? - How to fix this warning: “React Hook useEffect has a missing dependency: 'history'”? 我应该如何修复 eslint 警告(React Hook useEffect 缺少依赖项)和由警告引起的循环? - How should I fix eslint warning (React Hook useEffect has a missing dependency) and a loop caused by the warning? 如何修复“React Hook useEffect 缺少依赖项。 要么包含它,要么删除依赖数组”的问题? - How can I fix “React Hook useEffect has a missing dependency. Either include it or remove the dependency array” problem? 如何解决“React Hook useEffect 缺少依赖项。包含它或删除依赖项数组”的问题? - How to fix "React Hook useEffect has a missing dependency. Either include it or remove the dependency array" problem? 如何修复 useEffect 缺少依赖项“Dispatch” - how to fix useEffect has a missing dependency "Dispatch" React Hook useEffect 缺少对 useEffect 的依赖 - React Hook useEffect has a missing dependency with useEffect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM