data list are not keeping the old elements when new ones are added

Viewed 27

I've an upload list, the old data already in the list are removing from the list if I upload new data into the list. But I want to list all the data together. How can I fix this issue?


const [fileList, setFileList] = useState<AddedFile[]>([]);

const beginUpload = (file: File[]) => {
    const addedFiles = file
      ?.filter((item) => item !== null)
      .map((item): AddedFile => {
        if (item === null) {
          throw new Error('Item is null');
        }
        return {
          title: item?.name || '',
          fileName: item?.name || '',
        };
      });

    setFileList([...addedFiles, ...fileList]);

    file.forEach((item: File) => {
      onUpload(item, {
        title: item?.name || '',
        fileName: item?.name || '',
      }).catch((err) => {
        console.error(err);
      });
    });
  }


1 Answers
Related