I want the user to "upload" a file from their computer, store it in the browser (I guess) and display the image, without sending it to a server. Basically, I want this to happen (example on the website): https://www.javascripture.com/FileReader
This works by itself, but I am using react-dropzone and it's not working.
My code:
import React, { useCallback, useState } from "react";
import { useDropzone } from "react-dropzone";
import styles from "./dropzone.module.css";
import ImageZone from "../imagezone";
export default function Dropzone() {
const [path, setPath] = useState("");
const openFile = () => {
const reader = new FileReader();
reader.onload = () => {
const dataURL = reader.result;
setPath(dataURL);
};
};
const onDrop = useCallback(acceptedFiles => {
openFile();
}, []);
const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop });
return (
<div className={styles.mycontainer}>
<div className={styles.dropzone} {...getRootProps()}>
<input {...getInputProps()} />
{isDragActive ? (
<p>Drop the files here ...</p>
) : (
<p>Drag 'n' drop some files here, or click to select files</p>
)}
</div>
<ImageZone src={path}></ImageZone>
</div>
);
}
store it in the browser?openFilefunction you're creating a file reader and setting what it does when it's finished reading... but where are you actually giving it the file to read and calling the function to begin reading?setPathfunction look like?