reactjs - 如何在反应打字稿中将 ref 添加到 CSVLINK?
问题描述
从服务器加载数据后,我想单击 CSV 下载链接。我尝试了https://github.com/react-csv/react-csv/issues/237中的所有方法
const csvLinkRef = React.useRef<CSVLink & HTMLAnchorElement & { link?: HTMLAnchorElement }>();
<CSVLink
ref={csvLinkRef}
data={tableDataToDownLoadAsCSV}
/>
解决方案
看看这段代码,它将有助于调整你的代码
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
推荐阅读
- javascript - 寻找一种绕过 querySelector 的方法,只需选择第一个元素或类
- python - 出于某种原因,我的整个等值线都是灰色的。使用来自 NYC 的 folium、一个小型数据框和一个邮政编码 geojson
- mysql - 通过 Node.js 连接 MySQL
- tizen - 三星 Gear 3 中的心率传感器
- go - func (e *errorString) FormatError(p Printer) (下一个错误)
- stack - aarch64 内联汇编堆栈指针约束内存地址与 Clang 6+ 的偏移量
- ruby-on-rails - Rails Active Storage:获取附件的相对磁盘服务路径
- javascript - AngularJS 从前面而不是整个属性过滤搜索词
- python - 如何在 python 中制作 9x9 网格并在控制台而不是 GUI 中查看,因为我想制作 Soduko 游戏?
- javascript - 在 Firestore 中创建密钥