| 1 |
By providing `maxFiles` prop you can limit how many files the dropzone accepts. |
| 2 |
|
| 3 |
**Note** that this prop is enabled when the `multiple` prop is enabled. |
| 4 |
The default value for this prop is 0, which means there's no limitation to how many files are accepted. |
| 5 |
|
| 6 |
|
| 7 |
```jsx harmony |
| 8 |
import React from 'react'; |
| 9 |
import {useDropzone} from 'react-dropzone'; |
| 10 |
|
| 11 |
function AcceptMaxFiles(props) { |
| 12 |
const { |
| 13 |
acceptedFiles, |
| 14 |
fileRejections, |
| 15 |
getRootProps, |
| 16 |
getInputProps |
| 17 |
} = useDropzone({ |
| 18 |
maxFiles:2 |
| 19 |
}); |
| 20 |
|
| 21 |
const acceptedFileItems = acceptedFiles.map(file => ( |
| 22 |
<li key={file.path}> |
| 23 |
{file.path} - {file.size} bytes |
| 24 |
</li> |
| 25 |
)); |
| 26 |
|
| 27 |
const fileRejectionItems = fileRejections.map(({ file, errors }) => { |
| 28 |
return ( |
| 29 |
<li key={file.path}> |
| 30 |
{file.path} - {file.size} bytes |
| 31 |
<ul> |
| 32 |
{errors.map(e => <li key={e.code}>{e.message}</li>)} |
| 33 |
</ul> |
| 34 |
|
| 35 |
</li> |
| 36 |
) |
| 37 |
}); |
| 38 |
|
| 39 |
|
| 40 |
return ( |
| 41 |
<section className="container"> |
| 42 |
<div {...getRootProps({ className: 'dropzone' })}> |
| 43 |
<input {...getInputProps()} /> |
| 44 |
<p>Drag 'n' drop some files here, or click to select files</p> |
| 45 |
<em>(2 files are the maximum number of files you can drop here)</em> |
| 46 |
</div> |
| 47 |
<aside> |
| 48 |
<h4>Accepted files</h4> |
| 49 |
<ul>{acceptedFileItems}</ul> |
| 50 |
<h4>Rejected files</h4> |
| 51 |
<ul>{fileRejectionItems}</ul> |
| 52 |
</aside> |
| 53 |
</section> |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
<AcceptMaxFiles /> |
| 58 |
``` |