简体   繁体   English

文件正在替换文件,而不是在 React (Next.js) 中添加

[英]File is replacing file, not adding in React (Next.js)

Please review my code first.请先查看我的代码。

const test = () => {

const [files, setFiles] = useState ([]);

//I think I have to edit following statement.

const handleFile = (e) => {
   const newFiles = []
   for (let i=0; i < e.target.files.length; i++) {
       newFiles.push(e.target.files[i])
    }
        setFiles(newFiles)
};

return (

   {files.map((file, index) => (
     <div>
       <div key={index}>
         <p>
         {file.name}
         </p>
         <Button size='small' onClick={() => {deleteSelectedFile(file.name)}}>
         Delete
         </Button>
       </div>
     </div>
    ))}

    <div>
        <label onChange={handleFile}>
            <input type='file' multiple />+ Attach File
        </label>
    </div>

)

}

with handleFile statement, I can appropriately get the files.使用 handleFile 语句,我可以适当地获取文件。

However, when I upload one file at a time and then upload one file again,但是,当我一次上传一个文件然后再次上传一个文件时,

the file is not added.该文件未添加。 the file replaces file.文件替换文件。

There is not problem when I upload multiple time at once.当我一次上传多次时没有问题。

For example, I upload 'hello.jpg', and it renders well in the screen.例如,我上传了“hello.jpg”,它在屏幕上呈现得很好。 Then, I upload 'goodbye.jpg', it renders well, but 'goodbye.jpg' replaces 'hello.jpg'.然后,我上传了'goodbye.jpg',它渲染得很好,但是'goodbye.jpg'替换了'hello.jpg'。

Therefore, what I see is just 'goodbye.jpg' [button], not因此,我看到的只是 'goodbye.jpg' [按钮],而不是

'hello.jpg' [button] 'goodbye.jpg' [button] 'hello.jpg' [按钮] 'goodbye.jpg' [按钮]

. .

I want my files to stacked up, without replacing.我希望我的文件堆叠起来,而不是替换。

I need some wisdom!我需要一些智慧!

In my opinion, you only need to spread the prev state values and the files during the change event.在我看来,您只需要在更改事件期间传播 prev state 值和文件。

import { useState } from "react";

export default function App() {
  const [files, setFiles] = useState([]);

  const handleChange = (e) => {
    // This is what you need
    setFiles((prev) => [...prev, ...Object.values(e.target.files)]);
  };

  return (
    <div>
      <label onChange={handleChange}>
        <input type="file" multiple />
      </label>
      {files.map((file) => {
        return <p>{file.name}</p>;
      })}
    </div>
  );
}

how about you don't create a new variable for new files, you just set the state for the files since it's an array你不为新文件创建一个新变量怎么样,你只需为文件设置 state 因为它是一个数组

setFiles(oldFile => [...oldFile,e.target.files[i]]);

If it's possible you can drop a codepen link如果可能,您可以删除 codepen 链接

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

相关问题 将样式指南添加到 next.js(反应)返回错误:ENOENT:没有这样的文件或目录, - Adding style guide to next.js (react) returns Error: ENOENT: no such file or directory, 为 React 在 Next.js 中导入 MDB Sass 文件时出错 - Error on importing MDB Sass file in Next.js for React 如何在 React / next.js 上读取上传的 json 文件的内容 - How to read content of uploaded json file on react / next.js Next.js 独立不读取环境文件 - Next.js standalone not reading env file 我需要在 React (Next.js) 中有效地渲染一个 6000 记录的 Excel 或 html 文件 - I need to render a 6000 record Excel OR html file in React (Next.js) efficiently .mdx 文件中的 React 组件和数学表达式在 Next.js 应用程序中未正确呈现 - React components and math expressions in .mdx file are not rendering correctly in Next.js application 从 JSON 文件动态加载图像位置 - (找不到模块...)反应,Next.js - Loading image location from JSON file dynamically - (Cannot find module…) React, Next.js Next.js (React) 和 ScrollMagic - Next.js (React) & ScrollMagic 将表单输入写入 Javascript、Node.js、React.js、Next.js 中的单独文件时遇到问题 - Having trouble writing form input to a seperate file in Javascript, Node.js, React.js, Next.js .env 文件访问令牌适用于 API 而不是 Next.js 中的另一个 - .env file access token working for API but not another in Next.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM