Hope you help me with this, I'm using the useDropzone hook from react-dropzone and I don't know how to make a remove file button for each file.
How can I remove a single file?
This is my code:
function DragFile(props) {
const { acceptedFiles, rejectedFiles, getRootProps, getInputProps } = useDropzone({
accept: 'image/jpeg, image/png, .pdf',
maxSize: 3000000,
multiple: true
});
const acceptedFilesItems = acceptedFiles.map(file => (
<Col xs={12} md={4} key={file.path} className="card-file">
<div className="file-extension">{file.path.substring(file.path.indexOf('.') + 1)}</div>
<span>{file.path.substring(0, file.path.indexOf('.'))} <small>{(file.size / 1000).toFixed(2)} Kb</small></span>
<button className="delete">DeleteButton</button>
</Col>
));
const rejectedFilesItems = rejectedFiles.map(file => (
<li key={file.path}>
{file.path.substring(0, file.path.indexOf('.'))} - {file.size / 1000} Kb
</li>
));
return (
<div>
<div {...getRootProps({ className: 'dropzone drag-n-drop' })}>
<input id="file-claim" {...getInputProps()} />
<img src={uploadSrc} alt="Subir archivo" />
<p>Drag files here (PDF, JPG, PNG).</p>
</div>
<Row className="accepted-files">
{acceptedFilesItems}
</Row>
</div>
);
}
export default DragFile;
You've probably already got this working but you just need to attach this to the click handler:
const remove = file => {
const newFiles = [...files]; // make a var for the new array
newFiles.splice(file, 1); // remove the file from the array
setFiles(newFiles); // update the state
};
And pass the number in your map: acceptedFiles.map(file...
should be acceptedFiles.map((file, i)....
Then have <button type="button" onClick={() => remove(i)> DeleteButton</button>
where i
is the number of the file in the array.
I hope this will help you :
import React, { useState, useEffect, useCallback } from 'react'
import { useDropzone } from 'react-dropzone'
const CreateFileUpload = () => {
const onDrop = useCallback(acceptedFiles => {
// Do something with the files
}, [])
const { getRootProps, getInputProps, isDragActive, acceptedFiles } = useDropzone({ onDrop, accept: '.png, .jpeg' })
const files = acceptedFiles.map((file, i) => (
<li key={file.path} className="selected-file-item">
{file.path} <i className="fa fa-trash text-red" onClick={() => remove(i)}></i>
</li>
));
const remove = file => {
const newFiles = [...files]; // make a var for the new array
acceptedFiles.splice(file, 1); // remove the file from the array
};
return (
<div>
<div {...getRootProps()} className="dropzone-main">
<div
className="ntc-start-files-dropzone"
aria-disabled="false"
>
</div>
<button className="add-button" type="button">
<i className="fa fa-plus"></i>
</button>
<h3 className="upload-title">
<span></span>
</h3>
<input
type="file"
multiple=""
autocomplete="off"
className="inp-file"
// onChange={uploadFile}
multiple
{...getInputProps()}
/>
{isDragActive ?
<div></div>
:
<div>
<p> Upload files </p>
</div>
}
</div>
<aside>
{files.length > 0 ? <h5>Selected Files</h5> : <h5></h5>}
<ul>{files}</ul>
</aside>
</div>
)
}
export default CreateFileUpload
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With