the problem is that you are not passing a copy of the file it's a reference e.target.files all you need is to spread it so you copy it so when useEffect run and delete the reference you have a copy in the images state
import React, { useEffect, useState, useRef } from "react";
const Stack = () => {
const fileInputRef = useRef();
const [images, setImages] = useState([]);
const addImage = (e) => {
setImages([...images, ...e.target.files]);
};
useEffect(() => {
if (fileInputRef) fileInputRef.current.value = null;
}, [images]);
console.log(images);
return <input type="file" ref={fileInputRef} onChange={addImage} />;
};
export default Stack;
i add a demo on codesandbox here
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…