Code Monkey home page Code Monkey logo

Comments (9)

abdennour avatar abdennour commented on June 2, 2024 1

Hello guys, As mentioned in couple of years, the repository is open for candidates to maintain. Please apply!

from react-csv.

derekbwarren avatar derekbwarren commented on June 2, 2024

I am having the same issue. @abdennour any chance this can get fixed?

from react-csv.

spencerjsmall avatar spencerjsmall commented on June 2, 2024

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.

spencerjsmall avatar spencerjsmall commented on June 2, 2024
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.

devDroid avatar devDroid commented on June 2, 2024

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.

lwoydziak avatar lwoydziak commented on June 2, 2024

Also experiencing this issue.

from react-csv.

riccardolardi avatar riccardolardi commented on June 2, 2024

Same problem here.

from react-csv.

lemazza avatar lemazza commented on June 2, 2024

I'm still experiencing this issue. Is there a fix?

from react-csv.

leo-arrcus avatar leo-arrcus commented on June 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.