admin管理员组文章数量:1321436
I am using the react-dropzone basic example (react-dropzone basic)
import React from 'react';
import {useDropzone} from 'react-dropzone';
function Basic(props) {
const {acceptedFiles, getRootProps, getInputProps} = useDropzone();
const files = acceptedFiles.map(file => (
<li key={file.path}>
{file.path} - {file.size} bytes
</li>
));
return (
<section className="container">
<div {...getRootProps({className: 'dropzone'})}>
<input {...getInputProps()} />
<p>Drag 'n' drop some files here, or click to select files</p>
</div>
<aside>
<h4>Files</h4>
<ul>{files}</ul>
</aside>
</section>
);
}
<Basic />
This is working as expected, until I use the click method to add a file. When adding a file using click the acceptedFiles
do not register the file (I have added an onChange handler and event.target.files shows the file so it is defintely being added to the overall FileList
).
Due to this I cannot display the file added by click details in the Files <ul>
as I would with a dragged file. I assume the same would be true for fileRejections
but I have not implemented it yet.
I hope I'm missing something obvious as I would assume that dropzone handles both drag and click behaviour?
Appreciate someone pointing me in the right direction if possible.
I am using the react-dropzone basic example (react-dropzone basic)
import React from 'react';
import {useDropzone} from 'react-dropzone';
function Basic(props) {
const {acceptedFiles, getRootProps, getInputProps} = useDropzone();
const files = acceptedFiles.map(file => (
<li key={file.path}>
{file.path} - {file.size} bytes
</li>
));
return (
<section className="container">
<div {...getRootProps({className: 'dropzone'})}>
<input {...getInputProps()} />
<p>Drag 'n' drop some files here, or click to select files</p>
</div>
<aside>
<h4>Files</h4>
<ul>{files}</ul>
</aside>
</section>
);
}
<Basic />
This is working as expected, until I use the click method to add a file. When adding a file using click the acceptedFiles
do not register the file (I have added an onChange handler and event.target.files shows the file so it is defintely being added to the overall FileList
).
Due to this I cannot display the file added by click details in the Files <ul>
as I would with a dragged file. I assume the same would be true for fileRejections
but I have not implemented it yet.
I hope I'm missing something obvious as I would assume that dropzone handles both drag and click behaviour?
Appreciate someone pointing me in the right direction if possible.
Share Improve this question asked May 28, 2020 at 12:22 Sam ScholefieldSam Scholefield 1,2502 gold badges9 silver badges21 bronze badges2 Answers
Reset to default 3The above code handles both drag and drop, however it only gives you the latest added files and not the entire list of uploaded file.
To implement it correctly you can maintain a state and add an onDrop function to useDropzone in which you will append the files and update state
function Basic(props) {
const [files, setFiles] = React.useState([]);
const onDrop = React.useCallback(acceptedFiles => {
setFiles(prev => [...prev, ...acceptedFiles]);
}, []);
const { getRootProps, getInputProps } = useDropzone({ onDrop });
const fileList = files.map(file => (
<li key={file.path}>
{file.path} - {file.size} bytes
</li>
));
return (
<section className="container">
<div {...getRootProps({ className: "dropzone" })}>
<input {...getInputProps()} />
<p>Drag 'n' drop some files here, or click to select files</p>
</div>
<aside>
<h4>Files</h4>
<ul>{fileList}</ul>
</aside>
</section>
);
}
Working demo
There is nothing wrong in your code. You just need to properly click on the div.
Just add bit of style to the drag and drop area so that the drop zone is clear to both drop as well as click.
working demo is here
export default function Basic(props) {
const { acceptedFiles, getRootProps, getInputProps } = useDropzone();
const files = acceptedFiles.map(file => (
<li key={file.path}>
{file.path} - {file.size} bytes
</li>
));
return (
<section className="container">
<div
style={{
cursor: "pointer",
background: "gray",
height: "200px",
border: "2px dashed blue"
}}
{...getRootProps({ className: "dropzone" })}
>
<input {...getInputProps()} />
<p>Drag 'n' drop some files here, or click to select files</p>
</div>
<aside>
<h4>Files</h4>
<ul>{files}</ul>
</aside>
</section>
);
}
本文标签: javascriptreactdropzoneaccessing files added by click not dropStack Overflow
版权声明:本文标题:javascript - react-dropzone - accessing files added by click not drop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742102434a2420863.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论