Github url


by react-dropzone

react-dropzone /react-dropzone

Simple HTML5 drag-drop zone with React.js.

7.2K Stars 644 Forks Last release: 2 months ago (v11.0.1) MIT License 529 Commits 135 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

react-dropzone logo


npm Build StatuscodecovOpen CollectiveOpen Collective

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

Documentation and examples at Source code at


Install it from npm and include it in your React build process (using Webpack, Browserify, etc).

npm install --save react-dropzone


bash yarn add react-dropzone


You can either use the hook:

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

return (

<input> { isDragActive ? 

Drop the files here ...


Drag 'n' drop some files here, or click to select files

 ) } ```

**IMPORTANT**: Under the hood, this lib makes use of [hooks](, therefore, using it requires React

>= 16.8


Or the wrapper component for the hook: ```jsx static import React from 'react' import Dropzone from 'react-dropzone'

<dropzone ondrop="{acceptedFiles"> console.log(acceptedFiles)}&gt;
  {({getRootProps, getInputProps}) =&gt; (
        <p>Drag 'n' drop some files here, or click to select files</p>

**Warning**: On most recent browsers versions, the files given by


 won't have properties 




, see [this SO question]( and [this issue](

Furthermore, if you want to access file contents you have to use the [FileReader API](

```jsx static import React, {useCallback} from 'react' import {useDropzone} from 'react-dropzone'

function MyDropzone() { const onDrop = useCallback((acceptedFiles) =\> { acceptedFiles.forEach((file) =\> { const reader = new FileReader()

reader.onabort = () => console.log('file reading was aborted') reader.onerror = () => console.log('file reading has failed') reader.onload = () => { // Do whatever you want with the file contents const binaryStr = reader.result console.log(binaryStr) } reader.readAsArrayBuffer(file) })

}, []) const {getRootProps, getInputProps} = useDropzone({onDrop})

return (


Drag 'n' drop some files here, or click to select files

 ) } ```
## 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
``` : ```jsx static import React from 'react' import {useDropzone} from 'react-dropzone'

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

return (

Drag 'n' drop some files here, or click to select files

) } ```

Note that whatever other props you want to add to the element where the props from


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



jsx static
 console.log(event) })} /\>

In the example above, the provided


 handler will be invoked before the internal one, therefore, internal callbacks can be prevented by simply using [stopPropagation]( See [Events]( for more examples.

_Important_: if you ommit rendering an
``` and/or binding the props from ``` getInputProps() ``` , opening a file dialog will not be possible. ## Refs





accept a custom


(defaults to


) as one of the attributes passed down in the parameter.

This can be useful when the element you're trying to apply the props from either one of those fns does not expose a reference to the element, e.g.:

const StyledDiv = styled.div

// Some styling here

function Example() { const {getRootProps, getInputProps} = useDropzone() <styleddiv refkey:>
    <p>Drag 'n' drop some files here, or click to select files</p>
  </styleddiv>} ```

If you're working with [Material UI]( and would like to apply the root props on some component that does not expose a ref, use [RootRef](

```jsx static import React from 'react' import {useDropzone} from 'react-dropzone' import RootRef from '@material-ui/core/RootRef'

function PaperDropzone() { const {getRootProps, getInputProps} = useDropzone() const {ref, ...rootProps} = getRootProps()

<rootref rootref="{ref}">
      <p>Drag 'n' drop some files here, or click to select files</p>
  </rootref>} ```

_Important_: do not set the


 prop on the elements where 




 props are set, instead, get the refs from the hook itself:

```jsx static import React from 'react' import {useDropzone} from 'react-dropzone'

function Refs() { const { getRootProps, getInputProps, rootRef, // Ref to the

inputRef // Ref to the


} = useDropzone()

Drag 'n' drop some files here, or click to select files

} ```

If you're using the


component, though, you can set the


prop on the component itself which will expose the


prop that can be used to open the file dialog programmatically:

const dropzoneRef = createRef()

<dropzone ref="{dropzoneRef}">
  {({getRootProps, getInputProps}) =&gt; (
      <p>Drag 'n' drop some files here, or click to select files</p>
</dropzone> ```

## Testing



 makes some of its drag 'n' drop callbacks asynchronous to enable promise based 


 functions. In order to properly test this, you may want to utilize a helper function to run all promises like this:

js static const flushPromises = () => new Promise(resolve => setImmediate(resolve))

Example with [react-testing-library]( ```js static import React from 'react' import Dropzone from 'react-dropzone' import { fireEvent, render } from 'react-testing-library'

test('invoke onDragEnter when dragenter event occurs', async () =\> { const file = new File([JSON.stringify({ping: true})], 'ping.json', { type: 'application/json' }) const data = mockData([file]) const onDragEnter = jest.fn()

const ui = ( <dropzone ondragenter="{onDragEnter}">
      {({ getRootProps, getInputProps }) =&gt; (
    </dropzone> ) const { container } = render(ui) const dropzone = container.querySelector('div')

dispatchEvt(dropzone, 'dragenter', data) await flushPromises(ui, container)

expect(onDragEnter).toHaveBeenCalled() })

function flushPromises(ui, container) { return new Promise(resolve =\> setImmediate(() =\> { render(ui, { container }) resolve(container) }) ) }

function dispatchEvt(node, type, data) { const event = new Event(type, { bubbles: true }) Object.assign(event, data) fireEvent(node, event) }

function mockData(files) { return { dataTransfer: { files, items: =\> ({ kind: 'file', type: file.type, getAsFile: () =\> file })), types: ['Files'] } } } ```

_Note_: using [Enzyme]( for testing is not supported at the moment, see [#2011](

More examples for this can be found in


s own [test suites](
## Support

### Backers

Support us with a monthly donation and help us continue our activities. [[Become a backer](]


### Sponsors

Become a sponsor and get your logo on our README on Github with a link to your site. [[Become a sponsor](]


## License



We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.