
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
react-downloadfile-hook
Advanced tools
npm i react-downloadfile-hook
Simple React hook to handle data downloading as a file without any dependencies and with typescript support.
This is a recommended method to handle downloading. You need to provide required fileName
, format/mime-type
, data
properties into the hook.
import { useDownloadFile } from "react-downloadfile-hook";
const { linkProps } = useDownloadFile({
fileName,
format: "image/svg+xml",
data: `<svg><circle cx="50" cy="50" r="40"fill="red" /></svg>`,
});
<a {...linkProps}>Download SVG File</a>;
The simplest way to handle downloading. It also named as a "force download a file". It uses old-school method, adding a link into the DOM and click it.
import { useDownloadFile } from "react-downloadfile-hook";
const { downloadFile } = useDownloadFile({
fileName,
format: "image/svg+xml",
data: `<svg><circle cx="50" cy="50" r="40"fill="red" /></svg>`,
});
<Button onClick={downloadFile}>Download SVG File</Button>;
If your data is not a string
type (ArrayBuffer
, Uint8Array
, etc), you may need to replace the built-in handler.
You need provide onCreateBlob
callback that returns Blob
.
import { useDownloadFile } from "react-downloadfile-hook";
const { downloadFile, linkProps } = useDownloadFile({
fileName,
format: "image/svg+xml",
data: new Uint8Array([1, 2, 3]),
onCreateBlob: (data: uint8Array, format) => {
const arrayBuffer = uint8Array.buffer;
return new Blob([arrayBuffer], { type: format });
},
});
<a {...linkProps}>Download File</a>;
<Button onClick={downloadFile}>Download File</Button>;
FAQs
React hook to download data as file
We found that react-downloadfile-hook demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.