[英]TypeError: Invalid attempt to spread non-iterable instance and Synthetic Events
I am trying to build a csv file uploader using react.我正在尝试使用 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.
选择文件时,我收到“传播不可迭代实例的无效尝试”错误,我尝试用它设置状态。 This is my code that gives that error:
这是我的代码,给出了该错误:
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;
I assumed this was an issue with setting the state here in this onUpload function so I tried no setting the state here but then I just get a synthetic even error.我认为这是在这个 onUpload 函数中设置状态的问题,所以我尝试不在这里设置状态,但后来我得到了一个综合的偶数错误。 Can anyone tell me the best way to handle this sort of upload?
谁能告诉我处理这种上传的最佳方法?
First of all you are trying to spread null
value which will obviously fail (that's the initial value for file
in state).首先,您试图传播显然会失败的
null
值(这是状态file
的初始值)。
Secondly - e
isn't the file you are looking for, it's the Event object.其次 -
e
不是您要查找的文件,它是 Event 对象。 If you want to get the uploaded files, use如果要获取上传的文件,请使用
const csvFile = e.target.files;
instead, which will hold every file uploaded by user as an array.相反,它将把用户上传的每个文件保存为一个数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.