亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

類型錯誤:傳播不可迭代實例和綜合事件的無效嘗試

類型錯誤:傳播不可迭代實例和綜合事件的無效嘗試

茅侃侃 2023-04-01 15:05:36
我正在嘗試使用 React 構建一個 csv 文件上傳器。I am getting the "Invalid attempt to spread non-iterable instances" error when the file is selected and I try to set the state with it. 這是我給出該錯誤的代碼:const IFImport = (props) => {    const [file, setFile] = useState(null);    const [loading, setLoading] = useState(false);  const onUpload = async (e) => {     const csvFile = e;     console.log(csvFile)     setFile(...file, csvFile)  }  return (    <>      <ContentRow>        <h1>          <Link to={"/"}>            <Button color="link">&lt;</Button>          </Link>          Upload Enrollment Information          <ErrorList error={props.error} />        </h1>      </ContentRow>      <ContentRow>      <Label>Upload a CSV File for Enrollment</Label>          <FormGroup>            <div>    {file !== null ? <p>{file.name}</p> : ""}            </div>            <div>              <Input                type="file"                name="data.file"                multiple={false}                onChange={e => onUpload(e)}                accept="/csv"              />{" "}            </div>          </FormGroup>      </ContentRow>    </>  );};export default IFImport;我認為這是在此 onUpload 函數中設置狀態的問題,所以我嘗試不在此處設置狀態,但后來我只是得到一個合成偶數錯誤。誰能告訴我處理這種上傳的最佳方法?
查看完整描述

1 回答

?
呼喚遠方

TA貢獻1856條經驗 獲得超11個贊

首先,您試圖傳播null顯然會失敗的價值(這是file狀態的初始值)。

其次 -e不是您要查找的文件,而是 Event 對象。如果要獲取上傳的文件,請使用

const csvFile = e.target.files;

相反,它將把用戶上傳的每個文件保存為一個數組。


查看完整回答
反對 回復 2023-04-01
  • 1 回答
  • 0 關注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號