Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. You can programmatically invoke the default OS file prompt; just use the `open` method returned by the hook.
  2. **Note** that for security reasons most browsers require popups and dialogues to originate from a direct user interaction (i.e. click).
  3. 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.
  4. 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.
  5. ```jsx harmony
  6. import React from 'react';
  7. import {useDropzone} from 'react-dropzone';
  8. function Dropzone(props) {
  9. const {getRootProps, getInputProps, open, acceptedFiles} = useDropzone({
  10. // Disable click and keydown behavior
  11. noClick: true,
  12. noKeyboard: true
  13. });
  14. const files = acceptedFiles.map(file => (
  15. <li key={file.path}>
  16. {file.path} - {file.size} bytes
  17. </li>
  18. ));
  19. return (
  20. <div className="container">
  21. <div {...getRootProps({className: 'dropzone'})}>
  22. <input {...getInputProps()} />
  23. <p>Drag 'n' drop some files here</p>
  24. <button type="button" onClick={open}>
  25. Open File Dialog
  26. </button>
  27. </div>
  28. <aside>
  29. <h4>Files</h4>
  30. <ul>{files}</ul>
  31. </aside>
  32. </div>
  33. );
  34. }
  35. <Dropzone />
  36. ```
  37. Or use the `ref` exposed by the `<Dropzone>` component:
  38. ```jsx harmony
  39. import React, {createRef} from 'react';
  40. import Dropzone from 'react-dropzone';
  41. const dropzoneRef = createRef();
  42. const openDialog = () => {
  43. // Note that the ref is set async,
  44. // so it might be null at some point
  45. if (dropzoneRef.current) {
  46. dropzoneRef.current.open()
  47. }
  48. };
  49. // Disable click and keydown behavior on the <Dropzone>
  50. <Dropzone ref={dropzoneRef} noClick noKeyboard>
  51. {({getRootProps, getInputProps, acceptedFiles}) => {
  52. return (
  53. <div className="container">
  54. <div {...getRootProps({className: 'dropzone'})}>
  55. <input {...getInputProps()} />
  56. <p>Drag 'n' drop some files here</p>
  57. <button
  58. type="button"
  59. onClick={openDialog}
  60. >
  61. Open File Dialog
  62. </button>
  63. </div>
  64. <aside>
  65. <h4>Files</h4>
  66. <ul>
  67. {acceptedFiles.map(file => (
  68. <li key={file.path}>
  69. {file.path} - {file.size} bytes
  70. </li>
  71. ))}
  72. </ul>
  73. </aside>
  74. </div>
  75. );
  76. }}
  77. </Dropzone>
  78. ```