Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-use-downloader

Package Overview
Dependencies
Maintainers
2
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-use-downloader

Creates a download handler function and gives progress information

  • 1.2.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

react-use-downloader

Creates a download handler function with its progress information and cancel ability.

NPM


StatementsBranchesFunctionsLines
StatementsBranchesFunctionsLines

Table of Contents


Running example

Plain
Example
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://upload.wikimedia.org/wikipedia/commons/4/4d/%D0%93%D0%BE%D0%B2%D0%B5%D1%80%D0%BB%D0%B0_%D1%96_%D0%9F%D0%B5%D1%82%D1%80%D0%BE%D1%81_%D0%B2_%D0%BF%D1%80%D0%BE%D0%BC%D1%96%D0%BD%D1%8F%D1%85_%D0%B2%D1%80%D0%B0%D0%BD%D1%96%D1%88%D0%BD%D1%8C%D0%BE%D0%B3%D0%BE_%D1%81%D0%BE%D0%BD%D1%86%D1%8F.jpg';
  const filename = 'beautiful-carpathia.jpg';

  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:
keydescriptionarguments
sizesize in bytesn/a
elapsedelapsed time in secondsn/a
percentagepercentage in stringn/a
downloaddownload function handler(downloadUrl: string, filename: string, timeout?: number, overrideOptions?: UseDownloaderOptions)
cancelcancel function handlern/a
errorerror object from the requestn/a
isInProgressboolean denoting download statusn/a
const { size, elapsed, percentage, download, cancel, error, isInProgress } =
  useDownloader();

useDownloader(options?: UseDownloaderOptions) also accepts fetch's RequestInit options:

  • Ex.:
const { download } = useDownloader({
  mode: 'no-cors',
  credentials: 'include',
  headers: {
    Authorization: 'Bearer TOKEN',
  },
});

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Eric Semeniuc
Eric Semeniuc

🤔
davdi1337
davdi1337

💻 🐛
Mauro Stepanoski
Mauro Stepanoski

🤔 💻
Sam
Sam "Betty" McKoy

🐛
Peran Osborn
Peran Osborn

🐛 🤔
Marcos
Marcos

🐛 🤔
9swampy
9swampy

🐛 💻
Dave Carlson
Dave Carlson

🤔

This project follows the all-contributors specification. Contributions of any kind welcome!


License

react-use-downloader is MIT licensed.


This hook is created using create-react-hook.

FAQs

Package last updated on 17 Apr 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc