[英]Warning: Failed prop type: Invalid prop `children` of type `string` supplied to `Dropzone`, expected `function`
I am using simple Drag and Drop function via Dropzone but Somehow having this error of "Warning: Failed prop type: Invalid prop children
of type string
supplied to Dropzone
, expected function
." 我通过Dropzone使用简单的拖放功能,但不知何故有这个错误“警告:失败道具类型:提供给
Dropzone
的类型string
无效道具children
,预期function
。” I have no idea what it is Na d I have checked the problem doesn't lie anywhere else other than this page 我不知道它是什么Na d我已经检查过问题不在于除了这个页面以外的任何其他地方
import React, { Component } from "react";
import Dropzone from "react-dropzone";
const MaxSize = 1000000000; //
class DragAndDrop extends Component {
handleOnDrop = (files, rejectedFiles) => {
console.log(files);
console.log("rejected files are:", rejectedFiles);
if (rejectedFiles && rejectedFiles.length > 0) {
const currentRejectFile = rejectedFiles[0];
const currentRejectFileType = currentRejectFile.type;
const currentRejectFileSize = currentRejectFile.size;
if (currentRejectFileSize > MaxSize) {
alert(
"This file is not allowed. " +
currentRejectFileSize +
currentRejectFileType +
" too large"
);
}
}
if (files && files.length > 0) {
const currentFile = files[0];
const currentFileType = currentFile.type;
const currentFileSize = currentFile.size;
if (currentFileSize > MaxSize) {
alert(
"This file is not allowed. " +
currentFileSize +
currentFileType +
" too large"
);
}
}
};
render() {
return (
<div>
<h1>Drop </h1>
<Dropzone
onDrop={() => this.handleOnDrop()}
multiple={false}
maxSize={MaxSize}
>
Drop image here or click to upload
</Dropzone>
</div>
);
}
}
export default DragAndDrop;
What I want is a simple drap and drop or select and push some image 我想要的是一个简单的下拉或选择并推送一些图像
Try the following: 请尝试以下方法:
<Dropzone
onDrop={() => this.handleOnDrop()}
multiple={false}
maxSize={MaxSize}
>
{ () => Drop image here or click to upload }
</Dropzone>
Here is a useful link: https://www.robinwieruch.de/react-render-props-pattern/ 这是一个有用的链接: https : //www.robinwieruch.de/react-render-props-pattern/
Actually It didn't work as they changed the DOCS or maybe something else but I had to change all the code and replace it with this 实际上它没有工作,因为他们改变了DOCS或者其他东西,但我不得不改变所有代码并用它替换它
import React, { Component } from "react"; import Dropzone from "react-dropzone"; const maxSize = 1048576; //1mb class DragAndDrop extends Component { onDrop = acceptedFiles => { console.log(acceptedFiles); }; render() { return ( <div> <Dropzone onDrop={this.onDrop} accept="image/png, image/gif image/jpg"//whatever the file type needed minSize={0} maxSize={maxSize} multiple > {({ getRootProps, getInputProps, isDragActive, isDragReject, rejectedFiles }) => { const isFileTooLarge = rejectedFiles.length > 0 && rejectedFiles[0].size > maxSize; return ( <div {...getRootProps()}> <input {...getInputProps()} /> {isDragActive ? "Drop it when it's hot!" : "Click me or drag a file to upload!"} {isDragActive && !isDragReject && "Drop it like it's hot!"} {isDragReject && "File type not accepted, sorry!"} {isFileTooLarge && ( <div>File is too large.</div> )} </div> ); }} </Dropzone> </div> ); } } export default DragAndDrop;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.