【发布时间】:2016-05-10 10:23:28
【问题描述】:
我有一个输入,我正在尝试在选择特定文件扩展名时呈现特定的额外输入。因此,在这种情况下,选择CSV文件时,将出现额外的选项。我遇到的问题是我当前的代码在 React Uncaught Invariant Violation: input is a void element tag and must not have children or use props.dangerouslySetInnerHTML. Check the render method of EnhancedSwitch. 中产生错误@
这是我正在使用的当前代码:
render() {
return (
<Dialog
open={this.state.open}
title="Upload File"
actions={standardActions}
autoScrollBodyContent={true}
>
<br />
<div>
<form encType="multipart/form-data">
<input type="file" accept=".xls, .xlsx, .csv" id="file_to_upload" onChange={this.handleChange.bind(this)} />
</form>
</div>
<br />
<div>
{this.state.isCSV ? <CSVInputs /> : null}
</div>
</Dialog>
);
}
还有handleChange函数:
handleChange(e) {
let files = document.getElementById('file_to_upload').files;
let formData = new FormData();
let extension = files[0].name.split('.')[files[0].name.split('.').length - 1].toLowerCase();
for (let key in files) {
if (files.hasOwnProperty(key) && files[key] instanceof File) {
formData.append(key, files[key]);
}
}
if (extension === 'csv') {
console.log('show me the inputs');
this.setState({
isCSV: true,
disabled: false,
files: formData
});
} else {
this.setState({
disabled: false,
files: formData
});
}
}
有没有办法可以更改我的代码,以免出现此错误?
任何帮助将不胜感激
感谢您的宝贵时间
【问题讨论】:
-
我在其他项目中使用过它,但在这种情况下并不想这样做。有没有办法可以避免使用额外的组件?
-
刚刚用 react-dropzone 试了一下,还是一样的错误
-
你确定你得到的错误来自你分享的代码吗?:你的
<input>没有任何孩子,也没有危险地设置在任何地方。会不会来自<Dialog/>内部的渲染? -
当我执行
<input type="file">some text</input>之类的操作时,我可以重现反应错误。也许您的 CSVInputs 组件中有一些input标记,其中定义了子/文本?
标签: javascript input reactjs