dropzone.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { useCallback } from 'react';
  2. import { useDropzone } from 'react-dropzone';
  3. import { connect } from 'react-redux';
  4. import { actionUploadFile } from '../actions';
  5. export const MyDropzone = ({ maxFiles, onUpload, onSet }) => {
  6. const onDrop = useCallback(async acceptedFiles => {
  7. let files = []
  8. await Promise.all(acceptedFiles.map(async (file) => {
  9. let result = await onUpload(file)
  10. files.push(result)
  11. console.log(files)
  12. }))
  13. onSet(files)
  14. }, [onUpload, onSet])
  15. const { getRootProps, getInputProps } = useDropzone({ onDrop, maxFiles: maxFiles })
  16. // const files = acceptedFiles.map(file => (
  17. // <li key={file.path}>
  18. // {file.path} - {file.size} bytes
  19. // </li>
  20. // ))
  21. return (
  22. <div>
  23. <div {...getRootProps()}>
  24. <input {...getInputProps()} />
  25. <span>Drag 'n' drop here, or click to select</span>
  26. </div>
  27. </div>
  28. )
  29. }
  30. export const ConnectDropzone = connect(null, { onUpload: actionUploadFile })(MyDropzone)