| 1 |
The `useDropzone` hook just binds the necessary handlers to create a drag 'n' drop zone. |
| 2 |
Use the `getRootProps()` fn to get the props required for drag 'n' drop and use them on any element. |
| 3 |
For click and keydown behavior, use the `getInputProps()` fn and use the returned props on an `<input>`. |
| 4 |
|
| 5 |
Furthermore, the hook supports folder drag 'n' drop by default. See [file-selector](https://github.com/react-dropzone/file-selector) for more info about supported browsers. |
| 6 |
|
| 7 |
|
| 8 |
```jsx harmony |
| 9 |
import React from 'react'; |
| 10 |
import {useDropzone} from 'react-dropzone'; |
| 11 |
|
| 12 |
function Basic(props) { |
| 13 |
const {acceptedFiles, getRootProps, getInputProps} = useDropzone(); |
| 14 |
|
| 15 |
const files = acceptedFiles.map(file => ( |
| 16 |
<li key={file.path}> |
| 17 |
{file.path} - {file.size} bytes |
| 18 |
</li> |
| 19 |
)); |
| 20 |
|
| 21 |
return ( |
| 22 |
<section className="container"> |
| 23 |
<div {...getRootProps({className: 'dropzone'})}> |
| 24 |
<input {...getInputProps()} /> |
| 25 |
<p>Drag 'n' drop some files here, or click to select files</p> |
| 26 |
</div> |
| 27 |
<aside> |
| 28 |
<h4>Files</h4> |
| 29 |
<ul>{files}</ul> |
| 30 |
</aside> |
| 31 |
</section> |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
<Basic /> |
| 36 |
``` |
| 37 |
|
| 38 |
Dropzone with `disabled` property: |
| 39 |
|
| 40 |
```jsx harmony |
| 41 |
import React from 'react'; |
| 42 |
import {useDropzone} from 'react-dropzone'; |
| 43 |
|
| 44 |
function Basic(props) { |
| 45 |
const {acceptedFiles, getRootProps, getInputProps} = useDropzone({ |
| 46 |
disabled: true |
| 47 |
}); |
| 48 |
|
| 49 |
const files = acceptedFiles.map(file => ( |
| 50 |
<li key={file.name}> |
| 51 |
{file.name} - {file.size} bytes |
| 52 |
</li> |
| 53 |
)); |
| 54 |
|
| 55 |
return ( |
| 56 |
<section className="container"> |
| 57 |
<div {...getRootProps({className: 'dropzone disabled'})}> |
| 58 |
<input {...getInputProps()} /> |
| 59 |
<p>Drag 'n' drop some files here, or click to select files</p> |
| 60 |
</div> |
| 61 |
<aside> |
| 62 |
<h4>Files</h4> |
| 63 |
<ul>{files}</ul> |
| 64 |
</aside> |
| 65 |
</section> |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
<Basic /> |
| 70 |
``` |