useDropzone 钩子中的 fileRejections 和 isDragReject 的区别

发布于 2025-01-10 18:10:32 字数 1595 浏览 0 评论 0原文

我使用 fileRejections 来捕获大小大于 5MB 的文件,使用 isDragReject 来捕获非 csv 的文件,如下所示:-

const maxSize=5242880;
const CSVDropzone = () => {
  const classes = useStyles();


  const { getRootProps, getInputProps, fileRejections, isDragActive, isDragAccept, isDragReject } = useDropzone({
    onDrop,
    accept: 'text/csv',
    minSize: 0,
    maxSize,
  });
  let isFileTooLarge = fileRejections.length > 0 && fileRejections[0].errors.length;

  return (
    <Fragment>
      <Grid container {...getRootProps()} className={classes.dropzone}>
        <input {...getInputProps()} />
        {isDragActive ? (
          <p>Drop here ...</p>
        ) : (
          <Fragment>
            <BackupOutlinedIcon />
            <p>Drag and drop or click to browse</p>
          </Fragment>
        )}
      </Grid>
      <Grid container>      
        <Grid item xs={12}>
            {isFileTooLarge && (
            <Typography variant="caption">File is too large. Allowed maximum sixe is 5 Mb.</Typography>
            )}
            {isDragReject && (
            <Typography variant="caption">Incorrect file format. Please upload a CSV file. </Typography>
            )}
        </Grid>
      </Grid>
    </Fragment>
  );
};

问题: 拖动非 csv 文件时..直到拖动的时间发生这种情况时,我们会看到文件格式不正确当文件被删除时,我们会看到文件太大
我觉得由于 isDragReject 是 useDropzone 的内部组件,因此只有在放置正在进行时才为真。我希望它保持真实,直到下一次 onDrop。
有人可以提出建议吗?

I'm using fileRejections for catching files which have size larger than 5MB and isDragReject for files which are not csv like this:-

const maxSize=5242880;
const CSVDropzone = () => {
  const classes = useStyles();


  const { getRootProps, getInputProps, fileRejections, isDragActive, isDragAccept, isDragReject } = useDropzone({
    onDrop,
    accept: 'text/csv',
    minSize: 0,
    maxSize,
  });
  let isFileTooLarge = fileRejections.length > 0 && fileRejections[0].errors.length;

  return (
    <Fragment>
      <Grid container {...getRootProps()} className={classes.dropzone}>
        <input {...getInputProps()} />
        {isDragActive ? (
          <p>Drop here ...</p>
        ) : (
          <Fragment>
            <BackupOutlinedIcon />
            <p>Drag and drop or click to browse</p>
          </Fragment>
        )}
      </Grid>
      <Grid container>      
        <Grid item xs={12}>
            {isFileTooLarge && (
            <Typography variant="caption">File is too large. Allowed maximum sixe is 5 Mb.</Typography>
            )}
            {isDragReject && (
            <Typography variant="caption">Incorrect file format. Please upload a CSV file. </Typography>
            )}
        </Grid>
      </Grid>
    </Fragment>
  );
};

PROBLEM: when a non-csv file is dragged..Till the time that drag is happening, we'll see Incorrect file formatThe moment the file is dropped, we see File is too large

I feel that since isDragReject is internal to useDropzone, it is only true till the drop is in progress. I want it to stay true until the next onDrop.
Can someone suggest something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒绊 2025-01-17 18:10:32

我认为,状态变量可以在这里帮助你。

我希望它在下一次 onDrop 之前保持不变。
有人可以提出建议吗?

为了实现这一点,我们仅在 isDragActive 为 true 时更新其值。

例如,您可以声明一个状态变量,例如

const [isRejected, setIsRejected] = useState(false);

,在 return 语句之前,可以添加以下条件,

if (isDragActive && isRejected != isDragReject) {
    setIsRejected(isDragReject);
}

在此之后,您可以使用 isRejected 而不是 isDragReject渲染。

PS:我还是 React 的初学者,所以如果我在这里做出任何错误的假设,请纠正我。

I think, a state variable can help you here.

I want it to stay true until the next onDrop.
Can someone suggest something?

To achieve this, we will update its value only when isDragActive is true.

For example, you can declare a state variable like,

const [isRejected, setIsRejected] = useState(false);

and before the return statement, the following condition can be added,

if (isDragActive && isRejected != isDragReject) {
    setIsRejected(isDragReject);
}

After this, you can use isRejected instead of isDragReject for your rendering.

PS: I'm still a beginner in React, so please correct me if I am making any wrong assumptions here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文