| 1 |
You can programmatically invoke the default OS file prompt; just use the `open` method returned by the hook. |
| 2 |
|
| 3 |
**Note** that for security reasons most browsers require popups and dialogues to originate from a direct user interaction (i.e. click). |
| 4 |
|
| 5 |
If you are calling `open()` asynchronously, there’s a good chance it’s going to be blocked by the browser. So if you are calling `open()` asynchronously, be sure there is no more than *1000ms* delay between user interaction and `open()` call. |
| 6 |
|
| 7 |
Due to the lack of official docs on this (at least we haven’t found any. If you know one, feel free to open PR), there is no guarantee that **allowed delay duration** will not be changed in later browser versions. Since implementations may differ between different browsers, avoid calling open asynchronously if possible. |
| 8 |
|
| 9 |
```jsx harmony |
| 10 |
import React from 'react'; |
| 11 |
import {useDropzone} from 'react-dropzone'; |
| 12 |
|
| 13 |
function Dropzone(props) { |
| 14 |
const {getRootProps, getInputProps, open, acceptedFiles} = useDropzone({ |
| 15 |
// Disable click and keydown behavior |
| 16 |
noClick: true, |
| 17 |
noKeyboard: true |
| 18 |
}); |
| 19 |
|
| 20 |
const files = acceptedFiles.map(file => ( |
| 21 |
<li key={file.path}> |
| 22 |
{file.path} - {file.size} bytes |
| 23 |
</li> |
| 24 |
)); |
| 25 |
|
| 26 |
return ( |
| 27 |
<div className="container"> |
| 28 |
<div {...getRootProps({className: 'dropzone'})}> |
| 29 |
<input {...getInputProps()} /> |
| 30 |
<p>Drag 'n' drop some files here</p> |
| 31 |
<button type="button" onClick={open}> |
| 32 |
Open File Dialog |
| 33 |
</button> |
| 34 |
</div> |
| 35 |
<aside> |
| 36 |
<h4>Files</h4> |
| 37 |
<ul>{files}</ul> |
| 38 |
</aside> |
| 39 |
</div> |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
<Dropzone /> |
| 44 |
``` |
| 45 |
|
| 46 |
Or use the `ref` exposed by the `<Dropzone>` component: |
| 47 |
|
| 48 |
```jsx harmony |
| 49 |
import React, {createRef} from 'react'; |
| 50 |
import Dropzone from 'react-dropzone'; |
| 51 |
|
| 52 |
const dropzoneRef = createRef(); |
| 53 |
const openDialog = () => { |
| 54 |
// Note that the ref is set async, |
| 55 |
// so it might be null at some point |
| 56 |
if (dropzoneRef.current) { |
| 57 |
dropzoneRef.current.open() |
| 58 |
} |
| 59 |
}; |
| 60 |
|
| 61 |
// Disable click and keydown behavior on the <Dropzone> |
| 62 |
<Dropzone ref={dropzoneRef} noClick noKeyboard> |
| 63 |
{({getRootProps, getInputProps, acceptedFiles}) => { |
| 64 |
return ( |
| 65 |
<div className="container"> |
| 66 |
<div {...getRootProps({className: 'dropzone'})}> |
| 67 |
<input {...getInputProps()} /> |
| 68 |
<p>Drag 'n' drop some files here</p> |
| 69 |
<button |
| 70 |
type="button" |
| 71 |
onClick={openDialog} |
| 72 |
> |
| 73 |
Open File Dialog |
| 74 |
</button> |
| 75 |
</div> |
| 76 |
<aside> |
| 77 |
<h4>Files</h4> |
| 78 |
<ul> |
| 79 |
{acceptedFiles.map(file => ( |
| 80 |
<li key={file.path}> |
| 81 |
{file.path} - {file.size} bytes |
| 82 |
</li> |
| 83 |
))} |
| 84 |
</ul> |
| 85 |
</aside> |
| 86 |
</div> |
| 87 |
); |
| 88 |
}} |
| 89 |
</Dropzone> |
| 90 |
``` |