Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

react-dropzone

> 前端框架
Open source

Simple HTML5 drag-drop zone with React.js.

11.0K stars0 likes2 views
WebsiteGitHub

About

Simple HTML5 drag-drop zone with React.js.

react-dropzone

Simple React hook to create a HTML5-compliant drag'n'drop zone for files.

Documentation and examples at https://react-dropzone.js.org. Source code at https://github.com/react-dropzone/react-dropzone/.

Installation

Install it from npm. react-dropzone ships as ESM and CommonJS with TypeScript types included, and works with any modern bundler (Vite, webpack, Rspack, etc.).

bash
npm install react-dropzone

or:

bash
yarn add react-dropzone

Usage

You can either use the hook:

javascript
import React, {useCallback} from "react";
import {useDropzone} from "react-dropzone";

function MyDropzone() {
  const onDrop = useCallback(acceptedFiles => {
    // Do something with the files
  }, []);
  const {getRootProps, getInputProps, isDragActive} = useDropzone({onDrop});

  return (
    

  );
}

Or the wrapper component for the hook:

javascript
import React from "react";
import Dropzone from "react-dropzone";

<Dropzone onDrop={acceptedFiles => console.log(acceptedFiles)}>
  {({getRootProps, getInputProps}) => (
    <section>
      

    </section>
  )}
</Dropzone>;

If you want to access file contents you have to use the FileReader API:

…

Dropzone Props Getters

The dropzone property getters are just two functions that return objects with properties which you need to use to create the drag 'n' drop zone. The root properties can be applied to whatever element you want, whereas the input properties must be applied to an <input>:

javascript
import React from "react";
import {useDropzone} from "react-dropzone";

function MyDropzone() {
  const {getRootProps, getInputProps} = useDropzone();

  return (
    

  );
}

Note that whatever other props you want to add to the element where the props from getRootProps() are set, you should always pass them through that function rather than applying them on the element itself. This is in order to avoid your props being overridden (or overriding the props returned by getRootProps()):

javascript

  );
}

If you're using the <Dropzone> component, though, you can set the ref prop on the component itself which will expose the {open} prop that can be used to open the file dialog programmatically:

javascript
import React, {createRef} from "react";
import Dropzone from "react-dropzone";

const dropzoneRef = createRef();

<Dropzone ref={dropzoneRef}>
  {({getRootProps, getInputProps}) => (
    

  )}
</Dropzone>;

dropzoneRef.open();

Testing

react-dropzone makes some of its drag 'n' drop callbacks asynchronous to enable promise based getFilesFromEvent() functions. In order to test components that use this library, you need to use the react-testing-library:

…

NOTE: using Enzyme for testing is not supported at the moment, see #2011.

More examples for this can be found in react-dropzone's own test suites.

Caveats

Required React Version

React 18 or above is required because we use hooks (the lib itself is a hook).

File Paths

Files returned by the hook or passed as arg to the onDrop cb won't have the properties path or fullPath. For more inf check this SO question and this issue.

Tauri (and other native webviews)

Inside a Tauri webview, OS file drops are intercepted by the native window layer before the webview sees them, so the browser's drag 'n' drop events never fire and the dropzone appears to ignore drops. Disable Tauri's native handling to let react-dropzone work unchanged: set dragDropEnabled: false (Tauri v2) or fileDropEnabled: false (Tauri v1) in tauri.conf.json. If you instead need the absolute paths of dropped files, keep native handling on and drive your own state from Tauri's drag-drop event. See the Using with Tauri guide and #1316.

Not a File Uploader

This lib is not a file uploader; as such, it does not process files or provide any way to make HTTP requests to some server; if you're looking for that, checkout filepond or uppy.io.

Paste to Upload

Pasting files into the dropzone is supported out of the box: when the dropzone (or a focused child, e.g. a <textarea>) has focus and the user pastes files with Ctrl/Cmd+V - for instance a screenshot copied to the clipboard - those files run through the same accept, size and validator checks and the same onDrop/onDropAccepted/onDropRejected callbacks as a drop. Pastes that carry no files (plain text and the like) are ignored and left untouched, so pasting into inputs keeps working.

The paste is only received when the dropzone has focus, so pair it with autoFocus or let the user click/tab into it first. To disable paste handling, set noPaste:

javascript
import React from "react";
import {useDropzone} from "react-dropzone";

function MyDropzone() {
  const {getRootProps, getInputProps} = useDropzone({noPaste: true});

  return (
    

  );
}

Using <label> as Root

If you use <label> as the root element, the file dialog will be opened twice; see #1107 and #1432 why. A <label> natively forwards clicks to the <input> it wraps, so the dialog opens once from that and once from our own click handler. To avoid this, use noClick:

javascript
import React, {useCallback} from "react";
import {useDropzone} from "react-dropzone";

function MyDropzone() {
  const {getRootProps, getInputProps} = useDropzone({noClick: true});

  return (
    <label {...getRootProps()}>
      <input {...getInputProps()} />
    </label>
  );
}

Using open() on Click

If you bind a click event on an inner element and use open(), it will trigger a click on the root element too, resulting in the file dialog opening twice. To prevent this, use the noClick on the root:

javascript
import React, {useCallback} from "react";
import {useDropzone} from "react-dropzone";

function MyDropzone() {
  const {getRootProps, getInputProps, open} = useDropzone({noClick: true});

  return (
    

  );
}

Changing Props Before open()

Browsers only let you open the file dialog from within a short window after a user gesture (a "transient user activation"). Because of this, open() must run synchronously in the same event that the user triggered - it cannot wait for React to re-render.

So if you update a prop (e.g. accept) and call open() in the same handler, open() runs against the previous render, before the new prop is applied. Some browsers (notably Safari) may also drop the file dialog entirely, so it appears to require a second click. See #1188.

javascript
//  open() runs with the old `accept` - and may not open at all on Safari
function MyDropzone() {
  const [accept, setAccept] = useState({"image/*": []});
  const {getRootProps, getInputProps, open} = useDropzone({accept, noClick: true});

  const pickPdfs = () => {
    setAccept({"application/pdf": []}); // applied on the next render
    open(); // still uses {"image/*": []}
  };

  return (
    

  );
}

Instead, render one dropzone per set of props (each with its own open) and call the right one, so no state change has to land before opening:

javascript
function MyDropzone() {
  const images = useDropzone({accept: {"image/*": []}, noClick: true});
  const pdfs = useDropzone({accept: {"application/pdf": []}, noClick: true});

  return (
    

  );
}

Camera Option Missing on Android

On Android 13+, Chrome and Edge open the native Android photo picker whenever a file input's accept attribute resolves to only image and/or video types (e.g. accept={{"image/*": []}}). That picker only lists the gallery and files - it has no "take a photo"/camera action - so the camera appears to be missing. See #1417.

This is browser/OS behavior, not something the lib controls, and it's unrelated to how the <input> is styled: a plain <input type="file"> with no accept (or one that also allows a non-media type) falls back to the older chooser that does include the Camera app.

If you need the camera, use the capture attribute, which you can pass straight through getInputProps():

javascript
// Camera only (no gallery/files)
<input {...getInputProps({capture: "environment"})} />

To offer both the camera and the gallery, render one dropzone per input (each with its own open) and let the user pick which one to trigger:

…

File Dialog Cancel Callback

The onFileDialogCancel() cb is unstable in most browsers, meaning, there's a good chance of it being triggered even though you have selected files.

We rely on using a timeout of 300ms after the window is focused (the window onfocus event is triggered when the file select dialog is closed) to check if any files were selected and trigger onFileDialogCancel if none were selected.

As one can imagine, this doesn't really work if there's a lot of files or large files as by the time we trigger the check, the browser is still processing the files and no onchange events are triggered yet on the input. Check #1031 for more info.

Fortunately, there's the File System Access API, which is currently a working draft and some browsers support it (see browser compatibility), that provides a reliable way to prompt the user for file selection and capture cancellation.

Also keep in mind that the FS access API can only be used in secure contexts.

NOTE You can enable using the FS access API with the useFsAccessApi property: useDropzone({useFsAccessApi: true}).

File System Access API

When setting useFsAccessApi to true, you're switching to the File System API (see the file system access RFC).

What this essentially does is that it will use the showOpenFilePicker method to open the file picker window so that the user can select files.

In contrast, the traditional way (when the useFsAccessApi is not set to true or not specified) uses an <input type="file"> (see docs) on which a click event is triggered.

With the use of the file system access API enabled, there's a couple of caveats to keep in mind:

  1. The users will not be ab

Issues· 5 open

View all issuesOpen on GitHub
  • #1425

    [ENHANCEMENT] Allow removing files

    enhancementUpdated Apr 2, 2025

> Tags

TypeScriptdrag-and-dropfilefile-uploadreact

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 28, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架