-
-
Notifications
You must be signed in to change notification settings - Fork 264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
asyncOnClick = true together with onClick just doesnt work #350
Comments
I am having the same issue. @abdennour any chance this can get fixed? |
I'm having the same issue. Of course, when the download gets triggered a second (and third, fourth, etc.) time, the data gets loaded properly because it's been set to the state variable. Only workaround I can think of is to have a dummy wrapper button that makes the API call and sets it to the state, before programmatically triggering CSVLink. |
|
Having the same issue. I don't think refs will work, since they would get overwritten by the component's own |
Also experiencing this issue. |
…Click just doesnt work
Same problem here. |
I'm still experiencing this issue. Is there a fix? |
Hello guys, As mentioned in couple of years, the repository is open for candidates to maintain. Please apply! |
Have done a simple workaround to handle with the async call using ref. import React, { useEffect, useRef, useState } from "react";
import { CSVLink } from "react-csv";
export default function App() {
const [csvLoading, setCSVLoading] = useState(false);
const [csvData, setCSVData] = useState([]);
const ref = useRef();
useEffect(() => {
if (!csvLoading || csvData.length > 0) {
return;
}
// Replace this Promise with your API call
// Then setCSVData(newData), not the dummy data.
new Promise((r) => setTimeout(r, 2000))
.then((newData) => {
setCSVData([
{
why: "doesn't this",
feature: "work normally???",
}
]);
})
.catch((error) => {
console.log(error);
});
}, [csvLoading]);
useEffect(() => {
if (csvData.length > 0 && csvLoading) {
ref.current.link.click();
}
}, [csvData]);
return (
<div className="App">
<button
onClick={() => {
if (!csvLoading && csvData.length === 0) {
setCSVLoading(true);
}
}}
>
Download CSV
</button>
<CSVLink data={csvData} ref={ref} filename="data" />
</div>
);
} You can find the working example here: https://codesandbox.io/s/gallant-breeze-4ym2zd?file=/src/App.js |
Something is definitely off with the asyncOnClick. After my supabase call (which invokes a REST call), the file is instantly downloaded (see the logging line) with the empty state because the data will be put in the state in the next line. So even before i call done(true|false), this thing downloads.
I dont even know how this can be. I am just doing an API call and that triggers the download. And no, there is no throwing of an exception in the api call or anything like that.
Has anyone experienced something similar? Its super weird.
The text was updated successfully, but these errors were encountered: