Reputation: 2685
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.
Upvotes: 0
Views: 851
Reputation: 3868
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.
Upvotes: 1