简体   繁体   中英

React state not updating after await - useState

Im trying to update multiple file upload status but after await not updating to any state

 const uploadFiles = async () => {
        let tradeFileListNew = [...tradeFileList];
    
        for (const fileList of tradeFileListNew) {
          if (fileList.status == "not-yet") {
            let index = tradeFileListNew.indexOf(fileList);
            tradeFileListNew[index].status = "started";
        setTradeFilesToUpload(tradeFileListNew);
 
        const formData = new FormData();
        formData.append("file", fileList.file);
        formData.append("service_type", "attachments");

        let res = await api.uploadCompanyFile(formData);
        tradeFileListNew[index].status = "completed";
        setTradeFilesToUpload(tradeFileListNew);

        
      }
    }
  };

Notice you're calling setTradeFilesToUpload(tradeFileListNew); multiple times, and attempting to mutate tradeFileListNew in between. React is going to ignore the second call to setTradeFilesToUpload because it's passed the same reference, so it's assumed to be a no-op.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM