Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!