Table of Contents. Improve Article. Save Article. Like Article. How to trigger a file download when. Welcome to GeeksforGeeks. Download Image. By clicking the download button. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. When the button is clicked, it propagates the event several levels up to a stateful component and changes the state of allowDownload. This in turn triggers the rendering of a CSVDownload component, which makes a fetch to get the specified survey data and results in the CSV being downloaded.
A problem here is that the file name of the CSV cannot be specified. It also does not seem to reliably download the file each time. In fact, it only seems to do it on the first click. It does not seem to be pulling in the data either. I have considered taking an approach using the json2csv and js-file-download packages, but I was hoping to avoid using vanilla JS and stick to React only.
Is that an okay thing to be concerned about? It also seems like one of these two approaches should work. Has anyone tackled a problem like this before and have a clear suggestion on the best way to solve it?
There's a great answer to how to do this here on the react-csv issues thread. Our code base is written in the "modern" style with hooks. Here's how we adapted that example:. Have a standard onClick callback function on your button that prepares the json data you want to export to csv.
I have noticed that this question has been getting many hits over the past several months. In case others are still looking for answers, here is the solution which worked for me. A similar solution was posted here , albeit not entirely the same. It is worth reading. I would also recommend reading the documentation for refs in React. Refs are great for solving a variety of problems but should only be used when they have to be.
Concerning this solution here a little modified code below worked for me. It will fetch the data on click and download the file at first time itself.
In case, the button downloads an empty CSV file and on the second click it downloads the data from the previous fetch, put your this. How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.
Asked 2 years, 11 months ago. Active 3 months ago. Viewed 43k times. I appreciate any help. Thank you! Improve this question. JasonG JasonG 1 1 gold badge 2 2 silver badges 11 11 bronze badges. JavaScript Basics. JavaScript Best Practices.
JavaScript Design Patterns. Object-Oriented JavaScript. Functional Javascript. JavaScript Tips. React Show sub menu. React Answers.
React Projects. React Native. NativeScript React. React Tips. React D3. React Ionic. React Bootstrap. React Hooks. Material UI. Storybook for React. Top React Libraries. Shards React. React Suite. Vue Show sub menu. Vue Answers. Vue 3 Show sub menu. Vue 3 Testing.
Vue 3 Projects. Vue Testing. NativeScript Vue. Vue and D3. Top Vue Packages. Vue Ionic. Ant Design Vue. Chakra UI Vue. Nodejs Show sub menu. Angular Show sub menu.
Angular Material. TypeScript Show sub menu. TypeScript Best Practices.
0コメント