Comments (9)
Hello guys, As mentioned in couple of years, the repository is open for candidates to maintain. Please apply!
from react-csv.
I am having the same issue. @abdennour any chance this can get fixed?
from react-csv.
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.
from react-csv.
export default function DownloadBtn() {
const [csv, setCSV] = useState({ data: null, fileName: "" });
const csvLink = useRef(null);
const exportToCsv = async (layer) => {
const response = await fetch(`/actions/${layer.id}/csv`, {
method: "GET",
});
const data = await response.json();
const fileName = layer.name.replace(/\s/g, "") + Date.now() + ".csv";
setCSV({ data: data, fileName: fileName });
csvLink.current.link.click();
};
return (
<>
<button
onClick={() => exportToCsv(layer)}
className="btn"
>
CSV
</button>
{csv.data && (
<CSVLink
ref={csvLink}
data={csv.data}
filename={csv.fileName}
target="_blank"
/>
)}
</>
);
}
from react-csv.
Having the same issue. I don't think refs will work, since they would get overwritten by the component's own ref
.
from react-csv.
Also experiencing this issue.
from react-csv.
Same problem here.
from react-csv.
I'm still experiencing this issue. Is there a fix?
from react-csv.
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
from react-csv.
Related Issues (20)
- Can we download multiple tables in same file? HOT 1
- Start download while data is still being fetched from API calls HOT 1
- How to get it to ignore commas in strings within an array
- how to new line in cell HOT 1
- still getting UTF-8-BOM when setting prop uFEFF={false} HOT 1
- is there any condition ' . ' must after ' " ' ? (need full stop after closing of double inverted comma)
- When the text contains double quotes, it will be divided into different columns. HOT 5
- File .csv not open directly with Excel HOT 1
- Data display vertically instead
- Module not found: Can't resolve 'prop-types' for NextJS project HOT 3
- Issue in async data
- [Object object] alert coming on CSVLink click
- The downloaded file is corrupted and cannot be open by microsoft excel
- Specify column order but not write headers
- Getting empty CSV files after upgrading to React 18 HOT 4
- Async Data HOT 3
- Detect the correct seperator
- Zip multiple files
- data attribute doesn't support function HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-csv.