2

I am having this problem to build my app. Anyone knows what is wrong?

React Hook useEffect has a missing dependency: 'conectar'. Either include it or remove the dependency array 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 (....)
};
0

1 Answer 1

2

Your hook depends on the function connectar which is declared outside the hook, but is internal to the render process. It is re-manufactured on every render. Therefore, React sees it as a volatile dependency. 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.

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

  conectar();
}, [props]); 
Sign up to request clarification or add additional context in comments.

7 Comments

The first scenario would be a pointless exercise given conectar is created on every render.
Yes, I'm about to remove it.
you have missing denpendency such as setEquivalenteNovos, setData and ConexaoGraficoEquivalenteNovo
@Vencovsky State hooks are not considered dependencies. ConexaoGraficoEquivalenteNovo is imported from outside the function component so it's not a dependency. It must be some other problem.
Maybe ConexaoGraficoEquivalenteNovo aren't a dependecy, but the other two are
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.