I want to upload images in a list using input file and display it.
My code displays the 1st upload but after the 2nd upload it doesn’t display.
function DisImg() {
const [file, setFile] = useState([]);
function handleChange(e) {
let sel = URL.createObjectURL(e.target.files[0]);
setFile(prevV => [
...prevV, sel
]);
}
return (
<>
<Input type="file" onChange={handleChange}>Choose file</Input>
{
file.map(() => {
return <img src={file} alt="documents" height="50px" width="50px" />
})
}
</>
);
}
2
Answers
You’re not using the values stored in the
file
array. You passfile
directly to thesrc
of the image..map provides you with the current value when you’re mapping over the
file
array.add source variable like
url
, also addindex
variable as a uniquekey
that will help to track.