简体   繁体   English

React Hook useEffect 缺少依赖项

[英]React Hook useEffect has a missing dependency

I am having this problem to build my app.我在构建我的应用程序时遇到了这个问题。 Anyone knows what is wrong?有谁知道出了什么问题?

React Hook useEffect has a missing dependency: 'conectar'. React Hook useEffect 缺少一个依赖项:'conectar'。 Either include it or remove the dependency array react-hooks/exhaustive-deps包括它或删除依赖数组 react-hooks/exhaustive-deps

const GraficoEquivalenteNovo = props => {
  const [equivalenteNovos, setEquivalenteNovos] = useState([]);
  const [data, setData] = useState([]);
  async function conectar() {
    const resposta = await ConexaoGraficoEquivalenteNovo(props);
    setEquivalenteNovos(resposta[0]);
    setData(resposta[1]);
  }
  useEffect(() => {
    conectar();
  }, [props]);

  return (....)
};

Your hook depends on the function connectar which is declared outside the hook, but is internal to the render process.您的钩子取决于在钩子外部声明的函数connectar ,但在渲染过程内部。 It is re-manufactured on every render.它在每次渲染时重新制造。 Therefore, React sees it as a volatile dependency.因此,React 将其视为 volatile 依赖项。 You could have the function outside your component but since the function itself uses state hooks and depends on props, move it into the effect hook.您可以在组件外部使用该函数,但由于该函数本身使用状态挂钩并依赖于 props,请将其移动到效果挂钩中。

useEffect(() => {
   async function conectar() { 
    const resposta = await ConexaoGraficoEquivalenteNovo(props); 
    setEquivalenteNovos(resposta[0]);
    setData(resposta[1]);
  } 

  conectar();
}, [props]); 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM