useExport with download: false
When using useExport with option download: false, how do I get the CSV data?
useExport hook with the download option set to false and get the CSV data without triggering a download, you can follow these steps:useExport hook: Configure the useExport hook with the download option set to false.triggerExport function: Call the triggerExport function to initiate the export process. Since download is set to false, the data will not be downloaded automatically.triggerExport function will return the CSV data, which you can then handle as needed.useExport hook is configured with .triggerExport and logs the CSV data to the console. You can replace the console.log statement with any other logic to handle the CSV data as needed.download: falsehandleExportimport React from "react";
import { useExport } from "@refinedev/core";
interface IProduct {
id: number;
name: string;
description: string;
category: string;
}
export const HomePage = () => {
const { triggerExport, isLoading } = useExport<IProduct>({
resource: "products",
mapData: (item) => {
return {
...item,
category: JSON.stringify(item.category),
};
},
download: false, // Set download to false
});
const handleExport = async () => {
const csvData = await triggerExport();
console.log(csvData); // Handle the CSV data as needed
};
return (
<div
style={{
display: "flex",
flexDirection: "column",
alignItems: "flex-start",
padding: "16px",
}}
>
<div style={{ display: "flex", alignItems: "center", gap: "16px" }}>
<h2>Products</h2>
<button onClick={handleExport} disabled={isLoading}>
{isLoading ? "Exporting..." : "Export Products"}
</button>
</div>
</div>
);
};