javascript - react dropzone 与 react 图像文件调整器不起作用

标签 javascript reactjs async-await image-resizing react-dropzone

我正在与 react dropzone 合作和 react image file resizer当图像下降时上传多个调整大小的图像。
React Dropzone 工作正常(上传、显示缩略图),无需 react 图像文件调整器。
但是将两个模块组合在一起,当图像下降时,它可以很好地调整大小,但不知何故不会更新状态。
似乎是由于使用 async,await 的方式错误造成的,
因为finally子句中的console.log比try-await子句中的console.log先出现。
这是有问题的代码。

这是 react 图像文件调整器代码:

  const resizeFile = (file) =>
    new Promise((resolve) => {
      Resizer.imageFileResizer(
        file,
        300,
        300,
        "JPEG",
        80,
        0,
        (uri) => {
          resolve(uri);
        },
        "file"
      );
    });

这是react dropzone中的onDrop方法:

/// onDrop method in react dropzone
  const onDrop = async (images) => {
    let uploadBranchImages = [];
    try {
      await images.map((image) => {
        resizeFile(image).then((resizedImage) => {
          console.log(resizedImage);
          Object.assign(resizedImage, {
            preview: URL.createObjectURL(resizedImage),
          });
          uploadBranchImages.push(resizedImage);
        });
      });
    } catch (err) {
      console.log(err);
    } finally {
      console.log(uploadBranchImages);
      let tempBranchImages = [...state.branchImages, ...uploadBranchImages];
      console.log(tempBranchImages);

      setState({
        ...state,
        branchImages: tempBranchImages,
      });
    }
  };

在这段代码中,首先出现finally子句中uploadBranch的日志,接下来是tempBranchImages的日志以及try子句中resizedImage的日志最后。
另外,当我将 URL 放入 Chrome 地址栏中时,调整大小的图像显示得很好。

这是日志的捕获: capture_of_logs

所以问题是......为什么这些代码不能按顺序工作?
因此,上传后图像不会显示在拖放区中。

有关更多信息,我将附加日志而不使用 react 图像文件调整器。 与上面捕获的数组预览不同

( [ ] 与 [文件] )

这是日志: capture_of_logs_without_resize

任何建议将不胜感激..

最佳答案

非常感谢您的回答,这正是我想要的。如果有人想使用钩子(Hook) useDropzone():

const {getRootProps, getInputProps, isDragActive, isDragAccept, isDragReject} = useDropzone({
        accept: "image/*",
        maxFiles: 1,
        onDrop: async (acceptedFiles: any) => {
            await Promise.all(
                acceptedFiles.map((image: any) => {
                    return resizeFile(image);
                })
            ).then((uploadBranchImages) => {
                console.log('Upload Branch Images', uploadBranchImages);
                setImages(
                    uploadBranchImages
                        .map((image: any) => {
                                const imageModified = Object.assign(image, {
                                    preview: URL.createObjectURL(image)
                                })

                                handleFireBaseUpload(imageModified);
                                return imageModified;
                            }
                        )
                );

            })
        }
    });

关于javascript - react dropzone 与 react 图像文件调整器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67997639/

相关文章:

c# - ConfigureAwait(false) 仍然死锁

javascript - 从 react 开始

javascript - 将 .ogg blob 转换为 Google Drive 文件

javascript - 对 localhost 的 CORS https 访问在 Chrome 上有效,但在 Firefox 上无效

javascript - getCurrentPosition() 和 watchPosition() 在不安全的来源上被弃用

reactjs - 如何在react中使用hook router获取当前url路径?

dynamic - React 动态标签名称

c# - 使用异步创建隐式任务?

javascript - 代码始终在响应发送到浏览器后执行

reactjs - 在 react redux 应用程序中手动导航时状态丢失