简体   繁体   English

类型错误:传播不可迭代实例和合成事件的尝试无效

[英]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">&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;

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.

相关问题 未处理的拒绝(TypeError):散布不可迭代实例的无效尝试 - Unhandled Rejection (TypeError): Invalid attempt to spread non-iterable instance React Uncaught TypeError:传播不可迭代实例的无效尝试 - React Uncaught TypeError: Invalid attempt to spread non-iterable instance TypeError:传播不可迭代实例的无效尝试 - TypeError: Invalid attempt to spread non-iterable instance 传播不可迭代实例的尝试无效 - Invalid attempt to spread non-iterable instance 将项目添加到 Reducer 中的空数组中会给出错误“类型错误:传播不可迭代实例的尝试无效”。 - Add Items into Empty array in Reducer give Error 'TypeError: Invalid attempt to spread non-iterable instance.' 未处理的运行时错误类型错误:传播不可迭代实例的无效尝试 - Unhandled Runtime Error TypeError: Invalid attempt to spread non-iterable instance 在Flatlist React Native中传播不可迭代实例的无效尝试 - Invalid attempt to spread non-iterable instance in Flatlist React Native Redux Web扩展 - 未捕获的TypeError:无效尝试传播不可迭代的实例 - Redux Web Extension - Uncaught TypeError: Invalid attempt to spread non-iterable instance 类型错误:尝试修补 API 时尝试传播不可迭代实例无效 - TypeError: Invalid attempt to spread non-iterable instance when trying to patch API “TypeError:传播不可迭代实例的无效尝试”:将数组或 Object 添加到数组 - “TypeError: Invalid attempt to spread non-iterable instance”: Adding either an Array or Object to an Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM