npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

use-xhr

v0.0.5

Published

React hook for chunked file upload

Readme

Getting Started

To get it started, add use-xhr to your project:

npm install --save use-xhr

Please note that use-xhr requires react@^16.8.0 as a peer dependency.

Examples

Basic Usage

import useXhr from 'use-xhr';

function Upload({ url }) {
  const { getRootProps, getInputProps, isDragActive } = useXhr({
    getUploadParams: { url },
  });

  return (
    <div {...getRootProps()}>
      <input {...getInputProps()} />
      {isDragActive ? (
        <p>Drop the files here ...</p>
      ) : (
        <p>Drag 'n' drop some files here, or click to select files</p>
      )}
    </div>
  );
}

API

And their default values.

import useXhr from 'use-xhr';

function Upload() {
  const {
    files,
    rejectedFiles,
    extra: {
      accept,
      multiple,
      minSizeBytes,
      maxSizeBytes,
      maxFiles,
    },
    getRootProps,
    getInputProps,
    isDragActive,
    isDragReject,
    isFocus,
    inputRef,
    uploadAll,
  } = useXhr(
    {
      accept = '*',
      autoUpload = true,
      chunks = false,
      chunks = false,
      chunkSize = 512 * 1024,
      disabled = false,
      getChunkName = noop,
      getFilesFromEvent,
      getUploadParams = noop,
      maxFiles = Number.MAX_SAFE_INTEGER,
      maxSizeBytes = Number.MAX_SAFE_INTEGER,
      minSizeBytes = 0,
      multiple = true,
      noClick = false,
      timeout = 0,
      validate = noop,
      onFileUploaded = noop,
    }
  );

  return (
    // ...
  )
}

Props and Methods

accept: String

Set accepted file types. See https://github.com/okonet/attr-accept for more information. Keep in mind that mime type determination is not reliable across platforms.

autoUpload: Boolean

If set to true, files will be uploaded instantly after adding them. Otherwise you can trigger an upload manually by firing the restart method on the fileobject.

chunks: Boolean

If set to true uploaded files will be split into chunks.

chunkSize: Number

Size of single chunk (in bytes).

disabled: Boolean

Enable/disable upload.

getChunkName: Function

Argumens: fileWithMeta, chunkIndex

Use this to provide a custom name for each chunk.

getFilesFromEvent: Function

Argumens: event

Use this to provide a custom file aggregator.

getUploadParams: Function

Argumens: fileWithMeta

A callback that receives a fileWithMeta object and returns the params needed to upload the file.This prop is required to initiate useXhr.

It should return an object with { url (string), method (string), body, fields (object), headers (object), meta (object) }.

The only required key is url. POST is the default method.

If you pass your own request body, useXhr uploads it using xhr.send.

maxFiles: Number

Maximum of files.

maxSizeBytes: Number

Maximum file size (in bytes).

minFiles: Number

Minimum of files,

minSizeBytes: Number

Minimum file size (in bytes).

multiple: Boolean

Allow uploading (or selection from the file dialog) of multiple files.

noClick: Boolean

If true, disables click to open the native file selection dialog.

timeout: Number

A time in milliseconds. If the request does not succeed within the given time, it gets canceled. A falsy value will skip a timeout.

validate: Function

Argumens: fileWithMeta

A callback that receives a fileWithMeta object. If you return a falsy value from validate, the file is accepted, else it's rejected.

onFileUploaded: Function

Argumens: successFile, allFiles

A callback which is triggered after every single file upload. It receives the current uploaded fileWithMeta object and all files in the upload queue.

The filesobject

The files object provides information about the current uploads and methods to cancel, restart or remove files.

[
  {
    meta: {
      name, // Filename
      size, // Filesize in bytes
      type, // Filetype
      lastModifiedDate,
      uploadedDate,
      progress, // Upload progress in percent
      id, // Unique file id
      estimated, // Estimated time until upload is finished
      status, // Current upload status
      previewUrl, // For images, from URL.createObjectURL
      width, // For images
      height, // For images
      videoWidth, // For videos
      videoHeight, // For videos
    },
    cancel, // A method to cancel upload
    restart, // A method to restart upload
    remove, // A method to remove file from upload list
  },
];

The rejectedFiles object

The rejectedFiles object contains a file and an errors property, which provides useful information why this file was rejected.

License

MIT

Releases

This is our current release process.

# This will update the `CHANGELOG`, bumping the version and git tags locally:
$ npm run release

# Push to Gitlab along with the new tag:
$ git push origin master --follow-tags

# Publish the new version to `npm`:
$ npm publish