I'm looking to create multiple dropzones in the same component with React Dropzone and I want to handle a file from each dropzone differently. This will be part of a form with many different dropzones that each have different parameters. To start, I am trying to pass some identifying information from a particular dropzone to the useDropzone hook so I can eventually use a different function to handle each dropzone.
How can I access the name property passed through the getRootProps function so I can handle each drop, or is there a better way to accomplish this entirely? In the code below, I was able to print the event to the console, but I can't find "testtesttest" as a value anywhere in the event object. I the props from getRootProps and getInputProps are overriding the name prop I've included, even though I also put it through the getInputProps function.
import React, {useState, useEffect} from 'react';
import {useDropzone} from 'react-dropzone'
import styled from 'styled-components';
const MasterDropzone = styled.div`
height: 150px;
width: 80%;
border: 3px dashed black;
`
function UploadMedia(){
const [masterFile, setMasterFile] = useState({
file: null,
preview: null
});
const {
getRootProps,
getInputProps,
} = useDropzone({
accept: '.jpeg,.png',
noClick: false,
noKeyboard: true,
onDrop: (acceptedFiles,rejected,event) => {
console.log(event)
setMasterFile({
...masterFile,
file: acceptedFiles[0]
})
}
});
useEffect(
() => {
if(!masterFile.file){
setMasterFile({
...masterFile,
preview: undefined
})
return
}
const objectUrl = URL.createObjectURL(masterFile.file)
setMasterFile({
...masterFile,
preview: objectUrl
})
return () => URL.revokeObjectURL(objectUrl)
},
[masterFile.file]
);
return (
<div>
<h1>Upload Media</h1>
{
masterFile.preview
?
<img width='300px' height='300px' src={masterFile.preview} />
:
<MasterDropzone name='testtesttest' {...getRootProps({name: 'testtesttest'})}>
<p>Drag file here or click to upload</p>
<input name='testtesttest'{...getInputProps({name: 'testtesttest'})} />
</MasterDropzone>
}
</div>
)
}
export default UploadMedia