Skip to content
This repository was archived by the owner on Jun 5, 2022. It is now read-only.

onErase method doesn't clean array #162

Open
salientknight opened this issue Apr 24, 2020 · 5 comments
Open

onErase method doesn't clean array #162

salientknight opened this issue Apr 24, 2020 · 5 comments

Comments

@salientknight
Copy link

salientknight commented Apr 24, 2020

When I click the X in the corner to remove an image the array then contains empty array elements and often keeps old files in the array.

I add an image and delete it (repeat) what I expect is to have an empty array. What I have instead is an 4 element array with two pictures and two empty elements.

I assume that I need to keep track of the pictures array myself, so normally I would filter the array based on the image name, but onDrop is passed is an empty array, so there is nothing to work with to clean up the array.

I plan on having more than one picture upload on the page, so I need to keep the array clean for processing and keep the indexes in tact so that images do not lose attachment to their instance. They each need to be collected separately because I need to add alt, caption, keywords etc...

This is what is passed to onDrop when you click the X
Screen Shot 2020-04-24 at 4 31 08 PM

This is what the array looks like after + - +- of images
Screen Shot 2020-04-24 at 4 29 44 PM

@lucianobagattin
Copy link

+1

@abdulghanitech
Copy link

yes, even I'm facing the same issue. The state is not getting updated correctly when we remove images.

@Berihugebre
Copy link

@salientknight I have the same issue. Did you find any solutions? appreciate your help

@unigazer
Copy link

unigazer commented Jan 9, 2021

If you are using the spread operator to add the object to the current state, don't use it. Instead of using the spread operator, just update the state like this:

const handleDrop = (picture, pictureDataURLs) => {
  setPictures(picture); // or pictureDataURLs
};

The onChange prop from the component already returns the correct number of objects in the callback, so you can just update the state without needing to include the "previous state". The component is an <input type="file" multiple /> HTML node, so it always returns the correct value(s), on every change.

@jysatuo
Copy link

jysatuo commented Jun 2, 2021

If you are using the spread operator to add the object to the current state, don't use it. Instead of using the spread operator, just update the state like this:

const handleDrop = (picture, pictureDataURLs) => {
  setPictures(picture); // or pictureDataURLs
};

The onChange prop from the component already returns the correct number of objects in the callback, so you can just update the state without needing to include the "previous state". The component is an <input type="file" multiple /> HTML node, so it always returns the correct value(s), on every change.

You are right,very appreciate it

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants