MCPcopy Index your code
hub / github.com/react-dropzone/react-dropzone

github.com/react-dropzone/react-dropzone @v15.0.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v15.0.0 ↗ · + Follow
97 symbols 170 edges 18 files 7 documented · 7% 171 cross-repo links updated 4mo agov15.0.0 · 2026-02-10★ 10,99164 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

react-dropzone logo

react-dropzone

npm Tests codecov Open Collective Backers Open Collective Sponsors Gitpod Contributor Covenant

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 and include it in your React build process (using Webpack, Browserify, etc).

npm install --save react-dropzone

or:

yarn add react-dropzone

Usage

You can either use the hook:

```jsx static 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 (

  <input {...getInputProps()} />
  {
    isDragActive ?

Drop the files here ...

:

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

  }

) }


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

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



        <input {...getInputProps()} />


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





    </section>
  )}
</Dropzone>

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 (

  <input {...getInputProps()} />

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

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

  return (



      <input {...getInputProps()} />


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 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()): ```jsx static

console.log(event), role: 'button', 'aria-label': 'drag and drop area', ... })} />


In the example above, the provided `{onClick}` handler will be invoked before the internal one, therefore, internal callbacks can be prevented by simply using [stopPropagation](https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation).
See [Events](https://react-dropzone.js.org#events) for more examples.

*Important*: if you omit rendering an `<input>` and/or binding the props from `getInputProps()`, opening a file dialog will not be possible.

## Refs
Both `getRootProps` and `getInputProps` accept a custom `refKey` (defaults to `ref`) 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:

```jsx static
import React from 'react'
import {useDropzone} from 'react-dropzone'
// NOTE: After v4.0.0, styled components exposes a ref using forwardRef,
// therefore, no need for using innerRef as refKey
import styled from 'styled-components'

const StyledDiv = styled.div`
  // Some styling here
`
function Example() {
  const {getRootProps, getInputProps} = useDropzone()
  <StyledDiv {...getRootProps({ refKey: 'innerRef' })}>
    <input {...getInputProps()} />


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


  </StyledDiv>
}

If you're working with Material UI v4 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()

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

</Paper>

}


**IMPORTANT**: do not set the `ref` prop on the elements where `getRootProps()`/`getInputProps()` 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 `<input>`
  } = useDropzone()



    <input {...getInputProps()} />


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





}

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:

```jsx static import React, {createRef} from 'react' import Dropzone from 'react-dropzone'

const dropzoneRef = createRef()

{({getRootProps, getInputProps}) => (

  <input {...getInputProps()} />

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

)}

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](https://github.com/testing-library/react-testing-library):
```js static
import React from 'react'
import Dropzone from 'react-dropzone'
import {act, fireEvent, render} from '@testing-library/react'

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 }) => (



          <input {...getInputProps()} />



      )}
    </Dropzone>
  )
  const { container } = render(ui)

  await act(
    () => fireEvent.dragEnter(
      container.querySelector('div'),
      data,
    )
  );
  expect(onDragEnter).toHaveBeenCalled()
})

function mockData(files) {
  return {
    dataTransfer: {
      files,
      items: files.map(file => ({
        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 react-dropzone's own test suites.

Caveats

Required React Version

React 16.8 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.

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.

Using \<label> as Root

If you use \<label> as the root element, the file dialog will be opened twice; see #1107 why. To avoid this, use noClick: ```jsx static import React, {useCallback} from 'react' import {useDropzone} from 'react-dropzone'

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

return ( ) }


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

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

  return (



      <input {...getInputProps()} />
      <button type="button" onClick={open}>
        Open
      </button>



  )
}

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 able to select directories 2. It requires the app to run in a secure context 3. In Electron, the path may not be set (see #1249)

Supported Browsers

We use

Extension points exported contracts — how you extend this code

DropzoneProps (Interface)
(no doc)
typings/react-dropzone.d.ts
FileError (Interface)
(no doc)
typings/react-dropzone.d.ts
FileRejection (Interface)
(no doc)
typings/react-dropzone.d.ts
DropzoneRef (Interface)
(no doc)
typings/react-dropzone.d.ts
DropzoneRootProps (Interface)
(no doc)
typings/react-dropzone.d.ts

Core symbols most depended-on inside this repo

composeEventHandlers
called by 10
src/utils/index.js
isDefined
called by 7
src/utils/index.js
composeHandler
called by 6
src/index.js
isEvtWithFiles
called by 5
src/utils/index.js
composeDragHandler
called by 4
src/index.js
stopPropagation
called by 4
src/index.js
isPropagationStopped
called by 4
src/utils/index.js
useDropzone
called by 3
src/index.js

Shape

Function 58
Class 20
Method 11
Interface 7
Enum 1

Languages

TypeScript100%

Modules by API surface

src/utils/index.js23 symbols
src/index.spec.js22 symbols
typings/tests/plugin.tsx15 symbols
src/index.js13 symbols
typings/react-dropzone.d.ts9 symbols
typings/tests/events.tsx3 symbols
typings/tests/basic.tsx3 symbols
typings/tests/all.tsx3 symbols
typings/tests/accept.tsx3 symbols
src/utils/index.spec.js2 symbols
typings/tests/hook.tsx1 symbols

Dependencies from manifests, versioned

@babel/cli7.16.8 · 1×
@babel/core7.16.12 · 1×
@babel/eslint-parser7.16.5 · 1×
@babel/plugin-external-helpers7.16.7 · 1×
@babel/plugin-proposal-do-expressions7.16.7 · 1×
@babel/plugin-proposal-export-default-from7.16.7 · 1×
@babel/plugin-proposal-logical-assignment-operators7.16.7 · 1×
@babel/plugin-proposal-nullish-coalescing-operator7.16.7 · 1×
@babel/plugin-proposal-optional-chaining7.16.7 · 1×
@babel/plugin-proposal-pipeline-operator7.16.7 · 1×
@babel/plugin-transform-runtime7.16.10 · 1×
@babel/preset-env7.16.11 · 1×

For agents

$ claude mcp add react-dropzone \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact