我正在使用 react-dropzone 在我的 react Web 应用程序中实现上传文件 dropzone,我打算向我的 .NET Core Web API 发送一个发布请求以解析文件并将其保存到数据库。我使用本教程作为指南,同时进行自己的调整以适应我的项目规范,并且我不断收到以下错误,我不确定如何修复:
警告:React.createElement:类型不应为 null、未定义、布尔值或数字。它应该是一个字符串(对于 DOM 元素)或一个 ReactClass(对于复合组件)。检查
Upload
.
此错误会阻止应用程序呈现组件。我研究了该错误并找到了以下答案,但我相信它们与我的问题无关。
- https://github.com/ReactTraining/react-router/issues/2220
- https://codereviewvideos.com/blog/warning-react-createelement/
请参阅下面的上传组件:
import React, { PropTypes, Component } from 'react';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import RaisedButton from 'material-ui/RaisedButton';
import Dropzone from 'react-dropzone';
import FontIcon from 'material-ui/FontIcon';
import { blue500 } from 'material-ui/styles/colors';
import { PageHeader, Panel } from 'react-bootstrap';
const request = require('superagent');
const apiBaseUrl = 'http://localhost:5000/api/';
const style = {
margin: 15,
};
const title = 'Upload';
class Upload extends Component {
constructor(props, context) {
super(props);
this.state = {
filesPreview: [],
filesToBeSent: [],
printcount: 10,
};
context.setTitle(title);
}
onDrop(acceptedFiles) {
console.log('Accepted files: ', acceptedFiles[0].name);
const filesToBeSent = this.state.filesToBeSent;
if (filesToBeSent.length < this.state.printcount) {
filesToBeSent.push(acceptedFiles);
const filesPreview = [];
Object.keys(filesToBeSent).forEach((key, i) => {
filesPreview.push(<div>
{filesToBeSent[i][0].name}
<MuiThemeProvider>
<a href=""><FontIcon
className="material-icons customstyle"
color={blue500}
styles={{ top: 10 }}
>clear</FontIcon></a>
</MuiThemeProvider>
</div>
);
});
this.setState({ filesToBeSent, filesPreview });
} else {
alert('You have reached the limit of printing files at a time');
}
}
handleClick(event) {
console.log('handleClick: ', event);
const self = this;
console.log('self: ', self);
if (this.state.filesToBeSent.length > 0) {
const filesArray = this.state.filesToBeSent;
const req = request.post(`${apiBaseUrl}fileupload`);
Object.keys(filesArray).forEach((key, i) => {
console.log('files', filesArray[i][0]);
req.attach(filesArray[i][0].name, filesArray[i][0]);
req.end((err, res) => {
if (err) {
console.log('error ocurred');
}
console.log('res', res);
alert('File printing completed');
});
});
} else {
alert('Please upload some files first');
}
}
render() {
return (
<div>
<div className="row">
<div className="col-lg-12">
<PageHeader>Upload Data</PageHeader>
</div>
</div>
<div className="row">
<div className="col-lg-12 col-md-8 col-sm-4">
<Panel
header={<span>
<i className="fa fa-location-arrow fa-fw" /> Drag
and drop your file here, or use the file browser:
</span>}
>
<div className="App col-lg-6 col-md-4 col-sm-2">
<Dropzone onDrop={(files) => this.onDrop(files)}>
<div>Try dropping some files here, or click to select files to upload.</div>
</Dropzone>
</div>
<div className="col-lg-6 col-md-4 col-sm-2">
Files to be printed are:
{this.state.filesPreview}
</div>
<MuiThemeProvider>
<RaisedButton
label="Print Files" style={style}
onClick={(event) => this.handleClick(event)}
/>
</MuiThemeProvider>
</Panel>
</div>
</div>
</div>
);
}
}
Upload.contextTypes = { setTitle: PropTypes.func.isRequired };
export default Upload;
提前谢谢你。任何帮助是极大的赞赏。