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

125
Visualizações
Including function in React useEffect Dependency Array causes Unwanted Function Runs

In my React Typescript app, I am using a useEffect to call eventTracker whenever wallet.address changes.

const wallet = useWallet();
const eventTracker = useAnalyticsEventTracker();

useEffect(() => {
  if (wallet?.address) {
    eventTracker('Wallet', 'Connect', wallet?.address);
    console.log(wallet.address);
  }
}, [wallet]);

Both my IDE and browser console are giving the warning

React Hook useEffect has a missing dependency: 'eventTracker'. Either include it or remove the dependency array.eslintreact-hooks/exhaustive-deps

I am unsure either my approach or the linter suggestion is correct, because adding eventTracker to the dependency array causes the function passed into useEffect to run 10 times when the application starts, instead of just once.

What do folks recommend be done here?


Here's the hook:

import ReactGA from 'react-ga';

const useAnalyticsEventTracker = () => {
  const eventTracker = (
    category: string,
    action: string,
    label: string,
  ) => {
    ReactGA.event({ category, action, label });
  };

  return eventTracker;
};

export default useAnalyticsEventTracker;
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Your linter suggestion is correct, you just have to apply some memoization to avoid re-rendering.

Upon each render, eventTracker is re-created because you call useAnalyticsEventTracker that creates a new callback. Therefore, having it included in the dependency array will cause re-triggers of your useEffect, as it keeps changing.

You can solve it in many ways. One way is to use useCallback in order to memoize this function so it will be created only 1 time:

const useAnalyticsEventTracker = () => {
  const eventTracker = useCallback((
    category: string,
    action: string,
    label: string,
  ) => {
    ReactGA.event({ category, action, label });
  }, [])
about 4 years ago · Juan Pablo Isaza Relatório

0

Like Barak said the eventTracker is recreated each render. The useffect get this initial version of the function which is being discarded each render.

Adding it as a dependency would trigger the use effect each render.

Alternatively, to memoization you could import useAnalyticsEventTracker & call it instead of declaring and calling eventTracker. That way the function is not being updated. Hope that helps!

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