react-use-upload
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Zero dependency, total control, file upload hook for React.

This has been rewritten into a single file for simplicity, and the function signature has gotten much smaller.

100% Test Coverage :)

What is it?

This is a simple hook for handling file uploads in React. It takes the simplest approach possible so that you have full control over the upload process, while still providing lots of help vs implementing this yourself.

It has upload progress due to using XHR, and can be used for uploading file direct to Google Cloud, AWS, etc.

Install

npm install react-use-upload

Usage

Here's a basic example of uploading a single file to a url

const MyComponent = () => {
  let [upload, { progress, done, loading }] = useUpload(({ files }) => ({
    method: "PUT",
    url: url,
    body: files[0],
  }));

  return (
    <div>
      {loading ? `${progress}% complete` : null}
      <input
        type="file"
        onChange={(e) => {
          if (e.target.files) {
            upload({ files: e.target.files });
          }
        }}
      />
    </div>
  );
};

Formdata

It's up to you to pass in formdata

const MyComponent = () => {
  let [upload, { progress, done, loading }] = useUpload(({ files }) => {
    let formData = new FormData();
    files.forEach((file) => formData.append(file.name, file));

    return {
      method: "PUT",
      url: url,
      body: formData,
    };
  });

  return (
    <div>
      {loading ? `${progress}% complete` : null}
      <input
        type="file"
        onChange={(e) => {
          if (e.target.files) {
            upload({ file: e.target.files[0] });
          }
        }}
      />
    </div>
  );
};

Adding headers

You can pass a custom headers object

const MyComponent = () => {
  let [upload, { progress, done, loading }] = useUpload(({ files }) => {
    let formData = new FormData();
    files.forEach((file) => formData.append(file.name, file));

    return {
      method: "PUT",
      url: url,
      body: formData,
      headers: { Authorization: "test" },
    };
  });

  return (
    <div>
      {loading ? `${progress}% complete` : null}
      <input
        type="file"
        onChange={(e) => {
          if (e.target.files) {
            upload({ file: e.target.files[0] });
          }
        }}
      />
    </div>
  );
};

Customizing the request

You have full access to the XHR object, so you can set withCredentials or anything else you'd like.

const MyComponent = () => {
  let [upload, { progress, done, loading }] = useUpload(({ files, xhr }) => {
    xhr.withCredentials = true;

    let formData = new FormData();
    files.forEach((file) => formData.append(file.name, file));

    return {
      method: "PUT",
      url: url,
      body: formData,
      headers: { Authorization: "test" },
    };
  });

  return (
    <div>
      {loading ? `${progress}% complete` : null}
      <input
        type="file"
        onChange={(e) => {
          if (e.target.files) {
            upload({ file: e.target.files[0] });
          }
        }}
      />
    </div>
  );
};

Signed uploads

Signed uploads to a storage bucket on AWS or similar service, usually require this flow:

  • Hit your own backend to generate a signed url
  • send the file to that signed url to upload direct
  • do something on your frontend after it finishes

Here's how simple it is with this hook

const MyComponent = () => {
  let [upload, { progress, done, loading }] = useUpload(async ({ files }) => {
    // This function is your request logic for getting a url
    let url = await getUploadUrl({
      caseId,
      fileName: files[0].name,
      contentType: files[0].type,
    });
    // returning undefined skips the upload logic, in case your `getUploadUrl` has an error
    if (!url) return;

    return {
      method: "PUT",
      url: url,
      // send a single file in the body to the storage bucker
      body: files[0],
    };
  });

  useEffect(() => {
    if (done) {
      //refetch the data on the page, or some other action so the user can see the upload completed
    }
  }, [done, refetch]);

  return (
    <div>
      {loading ? `${progress}% complete` : null}
      <input
        type="file"
        onChange={(e) => {
          if (e.target.files) {
            upload({ file: e.target.files[0] });
          }
        }}
      />
    </div>
  );
};

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.0697latest
1.0.0-beta23beta
0.5.0-test2test
0.5.02test2

Version History

VersionDownloads (Last 7 Days)Published
1.0.0697
1.0.0-beta23
1.0.0-beta13
0.5.1568
0.5.142
0.5.13-beta2
0.5.12-beta2
0.5.122
0.5.112
0.5.102
0.5.919
0.5.62
0.5.82
0.5.72
0.5.52
0.5.42
0.5.32
0.5.22
0.5.12
0.5.02
0.5.0-test2
0.4.02
0.3.02
0.2.02
0.1.12
0.1.02

Package Sidebar

Install

npm i react-use-upload

Weekly Downloads

137

Version

1.0.0

License

ISC

Unpacked Size

104 kB

Total Files

35

Last publish

Collaborators

  • zackify