I would like to translate the function below written in vanilla javascript to a react function. The function below allows a user to click on the image input and append the image to the textarea which holds a class ".editor"
function getImage() {
var file = document.querySelector("input[type=file]").files[0];
var reader = new FileReader();
let dataURI;
reader.addEventListener(
"load",
function() {
dataURI = reader.result;
const img = document.createElement("img");
img.src = dataURI;
document.querySelector(".editor").appendChild(img);
},
false
);
if (file) {
console.log("s");
reader.readAsDataURL(file);
}
}
This is what i done so far in my react component... I'm receiving the following error message(s)
TypeError: editor.push is not a function
Tools.js component:
function Toolbar() {
const dispatch = useDispatch();
let inputRef = useRef(null);
const editor = useRef(null);
const [selectedFile, setSelectedFile] = useState(null);
const imgChangeHandler = e => {
e.preventDefault();
setSelectedFile(e.target.files[0]);
let reader = new FileReader();
let dataURI = reader.result;
const img = React.createElement("img",{src: dataURI});
editor.push(img);
if(selectedFile) {
console.log("s");
reader.readAsDataURL(selectedFile)
}
};
Editor.js component:
<>
<div className="center">
<div className="editor" ref={editor} style={editor} contentEditable={true} suppressContentEditableWarning={true}>
<h1>{introText}</h1>
<p>{subText}</p>
</div>
</div>
</>