I am trying to select multiple images, and the images are being selected and stored in the state, but only one image is displayed.Below is the code for the same
//Selecting the image and displaying the image
<div className="md-form mb-5">
<input type="file" id="form29" name='images' onChange={e => pickImage(e)} className="form-control validate" multiple hidden/>
<button className="btn btn-outline-dark btn-sm" onClick={e => selectImage(e)}>Pick Image</button>
</div>
<div className="row text-center">
{image.length > 0 ?
image.map(img => (
<div className="col-md-4" key={img.url}>
<img src={img.url} alt={img.name} className="img-fluid"/>
<span style={{cursor: 'pointer'}}>
<i class="far fa-trash-alt" onClick={e => deleteImage(img.name, e)}></i>
</span>
</div>
)) : <p>No Images Picked</p>}
</div>
//Defining the state
const [addDiary, setAddDiary] = useState({date: null, content: null, images: []});
const [ image, setImage ] = useState([]);
//Defining the methods to store and delete image
const selectImage = (e) => {
document.getElementById('form29').click();
}
const deleteImage = (name, e) => {
const remainingImage = image.filter(img => {return img.name !== name});
setImage(remainingImage);
const remainingImageState = addDiary.images.filter(img =>
{
return img.name !== name});
setAddDiary({...addDiary, images: remainingImageState });
}
const pickImage = (e) => {
console.log(e, 'e obj')
for(const file of e.target.files) {
const reader = new FileReader();
reader.onload = (e) => {
setImage([...image, {url: URL.createObjectURL(file), name: file.name}]);
console.log(e.target.name)
setAddDiary({ ...addDiary, images: [...addDiary.images, file]});
};
reader.readAsDataURL(file);
}
}