gpt4 book ai didi

reactjs - 如何在 react typescript 中将 ref 添加到 CSVLINK?

转载 作者:行者123 更新时间:2023-12-05 02:02:44 25 4
gpt4 key购买 nike

我想在从服务器加载数据后单击 CSV 下载链接。我尝试了 https://github.com/react-csv/react-csv/issues/237 中的所有方法

const csvLinkRef = React.useRef<CSVLink & HTMLAnchorElement & { link?: HTMLAnchorElement }>();

<CSVLink
ref={csvLinkRef}
data={tableDataToDownLoadAsCSV}
/>

Its giving me following error

最佳答案

看看这段代码,它将有助于调整你的代码

export default function dataListPage(props: DataListProps) {
const csvLinkRef = useRef<
CSVLink & HTMLAnchorElement & { link: HTMLAnchorElement }
>(null); // setup the ref that we'll use for the hidden CsvLink click once we've updated the data

const [data, setData] = useState<dataTypeObj[]>([]);

const hanldeExportButton = async () => {
const data: dataTypeObj[] = await fetchData();
setData(data);
csvLinkRef?.current?.link.click();
};

return (
<CSVLink
data={data}
className="exportButton"
filename="file.csv"
ref={csvLinkRef}
>
</CSVLink>
<Button
color="primary"
variant="contained"
size="small"
startIcon={<GetAppIcon />}
onClick={hanldeExportButton}
>
Export As CSV
</Button>
);
} //func dataListPage end

关于reactjs - 如何在 react typescript 中将 ref 添加到 CSVLINK?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65611889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com