Skip to content

Instantly share code, notes, and snippets.

@hashihei
Last active May 8, 2022 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hashihei/87902b75a4b925ec9b3b6740bc6a99ee to your computer and use it in GitHub Desktop.
Save hashihei/87902b75a4b925ec9b3b6740bc6a99ee to your computer and use it in GitHub Desktop.
react-dropzone set useState
import React, { useCallback, useState, useContext } from 'react'
import { useDropzone } from 'react-dropzone'
import { Store } from '../store/index'
const dropAreaStyle = {
width: "100%",
height: "100px",
backgroundColor: "#eeeeee",
display: "flex",
alignItems: "center",
justifyContent: "space-around",
marginTop: "50px",
borderRadius: "15px",
};
const DroppableArea = () => {
const { globalState, setGlobalState } = useContext(Store)
const onDrop = useCallback(acceptedFiles => {
let imgsJSONdata = globalState.images;
const initialDataLength = globalState.images.dataContainer.length;
console.log("mainContainerList-before:" + imgsJSONdata.mainContainerList)
// Do someting with the files.
acceptedFiles.map((file, index) => {
const imgIndex = String(initialDataLength + index);
imgsJSONdata.dataContainer.push({imgsNo: imgIndex, imgsPath: '', imgsTEXT: '', imgsData: URL.createObjectURL(file)})
imgsJSONdata.mainContainerList.push(imgIndex)
});
console.log("mainContainerList-after:" + imgsJSONdata.mainContainerList)
setGlobalState({type: 'SET_IMAGES', payload: { images: imgsJSONdata }});
},[globalState]);
const { getRootProps, getInputProps, isDragActive } = useDropzone({onDrop})
return (
<div style={dropAreaStyle} { ...getRootProps() }>
<input { ...getInputProps() } />
{
isDragActive ?
<p>Drop the files here ...</p> :
<p>Drag 'n' drop some files here, or click to select files.</p>
}
</div>
)
}
export default DroppableArea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment