Закончилось решение этой проблемы путем создания компонента Dropzone и изменения того, что мне было нужно для каждой dropzone с помощью реквизита, благодаря предложению Альваро. Вот что я сделал
UploadMedia. js
import Dropzone from './Dropzone'
function UploadMedia({ title }){
const [masterFile, setMasterFile] = useState({
content: null,
preview: null
});
const [subtitleFile, setSubtitleFile] = useState({
content: null,
preview: null
})
const [posterImage, setPosterImage] = useState({
content: null,
preview: null
})
const [coverImage, setCoverImage] = useState({
content: null,
preview: null
})
const [featureImage, setFeatureImage] = useState({
content: null,
preview: null
})
const masterText = <p>Master Video File</p>
const subtitleText = <p>Subtitle File</p>
const posterText = <p>Poster Image</p>
const coverText = <p>Cover Photo</p>
const featureText = <p>Feature Photo</p>
async function handleSubmit(evt) {
evt.preventDefault()
console.log('handle files here')
}
return (
<Container>
<h1>Upload media for {title.titleName}</h1>
<Form onSubmit={handleSubmit}>
<Dropzone file={masterFile} setFile={setMasterFile} text={masterText} height='200px' width='70%'/>
<Dropzone file={subtitleFile} setFile={setSubtitleFile} text={subtitleText} height='100px' width='70%'/>
<Dropzone file={posterImage} setFile={setPosterImage} text={posterText} height='200px' width='100px'/>
<Dropzone file={coverImage} setFile={setCoverImage} text={coverText} height='150px' width='350px'/>
<Dropzone file={featureImage} setFile={setFeatureImage} text={featureText} height='200px' width='400px'/>
<button type="submit">Save And Upload</button>
</Form>
</Container>
)
}
export default UploadMedia
Dropzone. js
function Dropzone({file, setFile, height, width, text}){
const {
getRootProps,
getInputProps,
} = useDropzone({
acceptedFiles: '',
noClick: false,
noKeyboard: true,
onDrop: (acceptedFiles) => {
setFile({
...file,
content: acceptedFiles[0]
})
}
});
useEffect(
() => {
if(!file.content){
setFile({
...file,
preview: undefined
})
return
}
const objectUrl = URL.createObjectURL(file.content)
setFile({
...file,
preview: objectUrl
})
// this line prevents memory leaks, but also removes reference to the image URL
// google chrome will remove this automatically when current session ends
// return () => URL.revokeObjectURL(objectUrl)
},
[file.content]
);
return (
<Container height={height} width={width}>
{
file.preview
?
<img width='40px' height='40px' src={file.preview} />
:
<DropzoneContainer {...getRootProps()}>
{text}
<p>Drag file or click to upload file</p>
<input {...getInputProps()} />
</DropzoneContainer>
}
</Container>
)
}
export default Dropzone