useDropzone 钩子中的 fileRejections 和 isDragReject 的区别
我使用 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 format
The 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为,状态变量可以在这里帮助你。
为了实现这一点,我们仅在
isDragActive
为 true 时更新其值。例如,您可以声明一个状态变量,例如
,在 return 语句之前,可以添加以下条件,
在此之后,您可以使用
isRejected
而不是isDragReject
渲染。PS:我还是 React 的初学者,所以如果我在这里做出任何错误的假设,请纠正我。
I think, a state variable can help you here.
To achieve this, we will update its value only when
isDragActive
is true.For example, you can declare a state variable like,
and before the return statement, the following condition can be added,
After this, you can use
isRejected
instead ofisDragReject
for your rendering.PS: I'm still a beginner in React, so please correct me if I am making any wrong assumptions here.