react-use-downloader
Creates a download handler function with its progress information and cancel ability.
Table of Contents
Running example
Plain |
---|
![]() |
Preview! |
Install
npm install --save react-use-downloader
Usage
import React from "react";
import useDownloader from "react-use-downloader";
export default function App() {
const {
size,
elapsed,
percentage,
download,
cancel,
error,
isInProgress
} = useDownloader();
const fileUrl = "https://olavoparno.github.io/saywololo/sounds/Wololo1.wav";
return (
<div className="App">
<p>Download is in {isInProgress ? 'in progress' : 'stopped'}</p>
<button onClick={() => download(fileUrl, "filename")}>
Click to download the file
</button>
<button onClick={() => cancel()}>Cancel the download</button>
<p>
Download size in bytes {size}
</p>
<label for="file">Downloading progress:</label>
<progress id="file" value={percentage} max="100" />
<p>Elapsed time in seconds {elapsed}</p>
{error && <p>possible error {JSON.stringify(error)}</p>}
</div>
);
}
Documentation
useDownloader()
returns:
- An object with the following keys:
key | description | arguments |
---|---|---|
size | size in bytes | n/a |
elapsed | elapsed time in seconds | n/a |
percentage | percentage in string | n/a |
download | download function handler | (downloadUrl: string, filename: string) |
cancel | cancel function handler | n/a |
error | error object from the request | n/a |
isInProgress | boolean denoting download status | n/a |
const {
size,
elapsed,
percentage,
download,
cancel,
error,
isInProgress
} = useDownloader();
License
react-use-downloader is MIT licensed.
This hook is created using create-react-hook.