Gathering detailed insights and metrics for react-dropzone
Gathering detailed insights and metrics for react-dropzone
Gathering detailed insights and metrics for react-dropzone
Gathering detailed insights and metrics for react-dropzone
react-dropzone-component
A Dropzone Component for ReactJS
react-dropzone-esm
Simple HTML5 drag-drop zone with React.js
react-dropzone-uploader
React file dropzone and uploader: fully customizable, progress indicators, upload cancellation and restart, zero deps and excellent TypeScript support
@types/react-dropzone
Stub TypeScript definitions entry for react-dropzone, which provides its own types definitions
Simple HTML5 drag-drop zone with React.js.
npm install react-dropzone
Typescript
Module System
Min. Node Version
Node Version
NPM Version
97.1
Supply Chain
99.6
Quality
86
Maintenance
100
Vulnerability
100
License
JavaScript (95.2%)
TypeScript (4.7%)
Shell (0.11%)
Total Downloads
592,830,575
Last Day
724,107
Last Week
3,087,218
Last Month
13,446,421
Last Year
165,213,103
10,666 Stars
630 Commits
792 Forks
61 Watching
14 Branches
176 Contributors
Minified
Minified + Gzipped
Latest Version
14.3.5
Package Id
react-dropzone@14.3.5
Unpacked Size
553.26 kB
Size
126.06 kB
File Count
51
NPM Version
10.9.0
Node Version
20.18.0
Publised On
04 Nov 2024
Cumulative downloads
Total Downloads
Last day
-1.6%
724,107
Compared to previous day
Last week
-14%
3,087,218
Compared to previous week
Last month
6.9%
13,446,421
Compared to previous month
Last year
14.8%
165,213,103
Compared to previous year
3
1
66
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/.
Install it from npm and include it in your React build process (using Webpack, Browserify, etc).
1npm install --save react-dropzone
or:
1yarn add react-dropzone
You can either use the hook:
1import React, {useCallback} from 'react' 2import {useDropzone} from 'react-dropzone' 3 4function MyDropzone() { 5 const onDrop = useCallback(acceptedFiles => { 6 // Do something with the files 7 }, []) 8 const {getRootProps, getInputProps, isDragActive} = useDropzone({onDrop}) 9 10 return ( 11 <div {...getRootProps()}> 12 <input {...getInputProps()} /> 13 { 14 isDragActive ? 15 <p>Drop the files here ...</p> : 16 <p>Drag 'n' drop some files here, or click to select files</p> 17 } 18 </div> 19 ) 20}
Or the wrapper component for the hook:
1import React from 'react' 2import Dropzone from 'react-dropzone' 3 4<Dropzone onDrop={acceptedFiles => console.log(acceptedFiles)}> 5 {({getRootProps, getInputProps}) => ( 6 <section> 7 <div {...getRootProps()}> 8 <input {...getInputProps()} /> 9 <p>Drag 'n' drop some files here, or click to select files</p> 10 </div> 11 </section> 12 )} 13</Dropzone>
If you want to access file contents you have to use the FileReader API:
1import React, {useCallback} from 'react' 2import {useDropzone} from 'react-dropzone' 3 4function MyDropzone() { 5 const onDrop = useCallback((acceptedFiles) => { 6 acceptedFiles.forEach((file) => { 7 const reader = new FileReader() 8 9 reader.onabort = () => console.log('file reading was aborted') 10 reader.onerror = () => console.log('file reading has failed') 11 reader.onload = () => { 12 // Do whatever you want with the file contents 13 const binaryStr = reader.result 14 console.log(binaryStr) 15 } 16 reader.readAsArrayBuffer(file) 17 }) 18 19 }, []) 20 const {getRootProps, getInputProps} = useDropzone({onDrop}) 21 22 return ( 23 <div {...getRootProps()}> 24 <input {...getInputProps()} /> 25 <p>Drag 'n' drop some files here, or click to select files</p> 26 </div> 27 ) 28}
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>
:
1import React from 'react' 2import {useDropzone} from 'react-dropzone' 3 4function MyDropzone() { 5 const {getRootProps, getInputProps} = useDropzone() 6 7 return ( 8 <div {...getRootProps()}> 9 <input {...getInputProps()} /> 10 <p>Drag 'n' drop some files here, or click to select files</p> 11 </div> 12 ) 13}
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()
):
1<div 2 {...getRootProps({ 3 onClick: event => console.log(event), 4 role: 'button', 5 'aria-label': 'drag and drop area', 6 ... 7 })} 8/>
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.
See 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.
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:
1import React from 'react' 2import {useDropzone} from 'react-dropzone' 3// NOTE: After v4.0.0, styled components exposes a ref using forwardRef, 4// therefore, no need for using innerRef as refKey 5import styled from 'styled-components' 6 7const StyledDiv = styled.div` 8 // Some styling here 9` 10function Example() { 11 const {getRootProps, getInputProps} = useDropzone() 12 <StyledDiv {...getRootProps({ refKey: 'innerRef' })}> 13 <input {...getInputProps()} /> 14 <p>Drag 'n' drop some files here, or click to select files</p> 15 </StyledDiv> 16}
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:
1import React from 'react' 2import {useDropzone} from 'react-dropzone' 3import RootRef from '@material-ui/core/RootRef' 4 5function PaperDropzone() { 6 const {getRootProps, getInputProps} = useDropzone() 7 const {ref, ...rootProps} = getRootProps() 8 9 <RootRef rootRef={ref}> 10 <Paper {...rootProps}> 11 <input {...getInputProps()} /> 12 <p>Drag 'n' drop some files here, or click to select files</p> 13 </Paper> 14 </RootRef> 15}
IMPORTANT: do not set the ref
prop on the elements where getRootProps()
/getInputProps()
props are set, instead, get the refs from the hook itself:
1import React from 'react' 2import {useDropzone} from 'react-dropzone' 3 4function Refs() { 5 const { 6 getRootProps, 7 getInputProps, 8 rootRef, // Ref to the `<div>` 9 inputRef // Ref to the `<input>` 10 } = useDropzone() 11 <div {...getRootProps()}> 12 <input {...getInputProps()} /> 13 <p>Drag 'n' drop some files here, or click to select files</p> 14 </div> 15}
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:
1import React, {createRef} from 'react' 2import Dropzone from 'react-dropzone' 3 4const dropzoneRef = createRef() 5 6<Dropzone ref={dropzoneRef}> 7 {({getRootProps, getInputProps}) => ( 8 <div {...getRootProps()}> 9 <input {...getInputProps()} /> 10 <p>Drag 'n' drop some files here, or click to select files</p> 11 </div> 12 )} 13</Dropzone> 14 15dropzoneRef.open()
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:
1import React from 'react' 2import Dropzone from 'react-dropzone' 3import {act, fireEvent, render} from '@testing-library/react' 4 5test('invoke onDragEnter when dragenter event occurs', async () => { 6 const file = new File([ 7 JSON.stringify({ping: true}) 8 ], 'ping.json', { type: 'application/json' }) 9 const data = mockData([file]) 10 const onDragEnter = jest.fn() 11 12 const ui = ( 13 <Dropzone onDragEnter={onDragEnter}> 14 {({ getRootProps, getInputProps }) => ( 15 <div {...getRootProps()}> 16 <input {...getInputProps()} /> 17 </div> 18 )} 19 </Dropzone> 20 ) 21 const { container } = render(ui) 22 23 await act( 24 () => fireEvent.dragEnter( 25 container.querySelector('div'), 26 data, 27 ) 28 ); 29 expect(onDragEnter).toHaveBeenCalled() 30}) 31 32function mockData(files) { 33 return { 34 dataTransfer: { 35 files, 36 items: files.map(file => ({ 37 kind: 'file', 38 type: file.type, 39 getAsFile: () => file 40 })), 41 types: ['Files'] 42 } 43 } 44}
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.
React 16.8 or above is required because we use hooks (the lib itself is a hook).
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.
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.
If you use <label> as the root element, the file dialog will be opened twice; see #1107 why. To avoid this, use noClick
:
1import React, {useCallback} from 'react' 2import {useDropzone} from 'react-dropzone' 3 4function MyDropzone() { 5 const {getRootProps, getInputProps} = useDropzone({noClick: true}) 6 7 return ( 8 <label {...getRootProps()}> 9 <input {...getInputProps()} /> 10 </label> 11 ) 12}
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:
1import React, {useCallback} from 'react' 2import {useDropzone} from 'react-dropzone' 3 4function MyDropzone() { 5 const {getRootProps, getInputProps, open} = useDropzone({noClick: true}) 6 7 return ( 8 <div {...getRootProps()}> 9 <input {...getInputProps()} /> 10 <button type="button" onClick={open}> 11 Open 12 </button> 13 </div> 14 ) 15}
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})
.
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:
We use browserslist config to state the browser support for this lib, so check it out on browserslist.dev.
React Dropzone integrates perfectly with Pintura Image Editor, creating a modern image editing experience. Pintura supports crop aspect ratios, resizing, rotating, cropping, annotating, filtering, and much more.
Checkout the Pintura integration example.
Support us with a monthly donation and help us continue our activities. [Become a backer]
Become a sponsor and get your logo on our README on Github with a link to your site. [Become a sponsor]
react-dropzone.js.org hosting provided by netlify.
Checkout the organization CONTRIBUTING.md.
MIT
No vulnerabilities found.
Reason
4 commit(s) and 17 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
packaging workflow detected
Details
Reason
Found 8/21 approved changesets -- score normalized to 3
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
69 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More