I am trying to create a function that allows me to remove a file from an array of files using the index, onclick.
**input:** files to upload stored in the state using hooks
**output:** files to upload stored in the state minus the removed file
here is what I have so far I am wondering if I am on the right track or is there a better way to go about doing this.
// remove document from uploadFiles array
const removeFile = index => {
// take an array of files and select a file at a given index
// remove a file at a selected index
// reassign all the indexs
const selectFile = index;
setFilesToUpload(filesToUpload.filter(file => file.selectFile[index] !== selectFile));
};
return (
<div className={uploadStyles.fileContainer} key={uuid()}>
<div className={uploadStyles.fileTitle}>
<p>Document {(index += 1)}</p>
<SquareButton
label={"remove file"}
icon={"x-mark"}
small={true}
name={file.selectFile}
onClick={() => removeFile(index)}
/>
</div>
<p className={uploadStyles.filePath}>{file.path}</p>
<p className={uploadStyles.fileTS}>
{file.type}
{" "}|{" "}
{formatBytes()}
</p>
<div className={uploadStyles.progressLine} />
</div>
);
});
};