简体   繁体   English

React state 在等待后未更新 - useState

[英]React state not updating after await - useState

Im trying to update multiple file upload status but after await not updating to any state我正在尝试更新多个文件上传状态,但在等待未更新到任何 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);请注意,您正在调用setTradeFilesToUpload(tradeFileListNew); multiple times, and attempting to mutate tradeFileListNew in between.多次,并试图在两者之间改变tradeFileListNew 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. React 将忽略对setTradeFilesToUpload的第二次调用,因为它传递了相同的引用,所以它被假定为无操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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