Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
Why does React Linter complain about a const variable not being added to the dependency array?

If we have a function that fetches user data as such:

const fetchUserData = () => {
  /* code here */
}

And then we have a useEffect with an empty array so it runs once after the component has rendered:

useEffect(() => {
  fetchUserData();
}, []);

Then eslint/tslint throws this warning: React Hook useEffect has a missing dependency: 'fetchUserData'. Either include it or remove the dependency array. I get that this is good practice if say our function variable was a let or var but how would the fetchUserData variable change if it's declared with a const keyword.

Is this an error on the linter and we should just ignore it or is there something that I'm missing?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Whether the "variable"¹ referring the function is let, var, or const is irrelevant. Remember that every time your component needs to be rendered, your component function gets called again, which creates an entirely new execution context with its own const (or let or var) "variable" with a new fetchUserData function assigned to it. useEffect will only ever call the first one of those that's created (because you have a [] dependency array). For this specific situation it may be harmless, but in the general case you run the risk of having closures over stale data, hence the linter warning.

If you don't use any state or props in fetchUserData, you can relocate it inside the useEffect callback, which both gets rid of the error and avoids recreating a function on every render that you're not going to use. (If you do use state or props in fetchUserData, it probably shouldn't only be called once when the component is mounted, but each time the state/props it uses changes.)


¹ More generally, binding (a binding of a name to a storage slot containing a value in the lexical environment object for the execution context of the function call).

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda