我正在嘗試使用 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"><</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 函數中設置狀態的問題,所以我嘗試不在此處設置狀態,但后來我只是得到一個合成偶數錯誤。誰能告訴我處理這種上傳的最佳方法?
類型錯誤:傳播不可迭代實例和綜合事件的無效嘗試
茅侃侃
2023-04-01 15:05:36