using the react-csv package, I am loading data asynchronously and then trying to download the retrieved data from the server with one click. The problem I am currently facing is that the click is a step behind the current state. When the component first renders it downloads an empty CSV file and on the second click it downloads the data from the previous fetch, not the current/latest fetch.That is the same situation that is described here , but the solution mentioned there didn't work in my case : se here is the code am working on :
const [newData, setNewData] = useState([]);
const csvLink = React.createRef();
const csvExport = (
<div>
<Button className="pull-right title-button" onClick={getData}>
CSV Export
</Button>
<CSVLink data={newData} headers={headers} target="_blank" ref={csvLink}></CSVLink>
</div>
);
const getData = async () => {
await getRetailLinkOutcomes(auth, startDate, endDate, 0, 0, filters, sort, sortDirection, false).then((response) => {
setNewData(response.records);
csvLink.current.link.click();
});
};