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

152
Visualizações
onClick is getting triggerd multiple times react ref

I have a download functionality where I am downloading

const csvLink = createRef();

const dropdown = (
  <div onClick={refetch}>
    {safeLabel(messages.csv)}
    <Icon name="pull" />
    {isSuccess ? <CSVLink ref={csvLink} data={data?.data}></CSVLink> : null}
  </div>
);

const onsuccess = (response) => {
  csvLink?.current?.link?.click();
};

const { refetch, data, isSuccess } = useQuery(
  "apicall",
  () => fetchData(headers),
  {
    enabled: false,
    onSuccess: onsuccess
  }
);

So, here when I click on the icon pull that time query gets called and sends me a response which I am passing it to the csvLink and trying to download the csv. Now because of the csvLink?.current?.link?.click(); called in onsuccess of query this refetch is getting called in infinite loop. Where to call this click and how to fix this issue ?

Thanks.

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

0

You should use useRef instead of createRef

const csvLink = useRef();

createRef: function creates a new ref every re-render.

useRef: function uses the same ref throughout. It saves it's value between re-renders in a functional component and doesn't create a new instance of the ref for every re-render.

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