我正在尝试为我上传到服务器之前在模态之间传递的文件对象设置setState。 以下是我试图实现的代码。
const state = {
selectedDocument: {
file: {},
},
selectedFile: new File([''],''),
}; //state init in constructor
//method being called upon modal close and passes the selected file object.
openUploadDocumentModal(files) {
console.log('files', files); //getting the file object here.
const ds = new File([files[0]], files[0].name);
//tried setting directly doest work.
this
.setState({
selectedDocument: {
file: new File([files[0]], files[0].name),
},
selectedFile: new File([files[0]], files[0].name),
});
//tried setting using the react update addon, doesnt work
this
.setState(prevState => update(prevState,
{
selectedFile: { $set: new File([files[0]], files[0].name)}, // trying to set the file file here, get {} on output
showAddDocumentModal: { $set: false },
showUploadDocumentModal: { $set: true },
}
));
}
答案 0 :(得分:0)
将州设置为:
const state = {
selectedDocument: {
file: null,
},
selectedFile: null,
};
然后,将州设置为:
this
.setState({
selectedDocument: {
file: files[0],
//files is a FileList variable, either obtained from event.target.files if obtained from input
//or event.dataTransfer.files if obtained from drag and drop
//or any other method
},
selectedFile: files[0],
});
就这么简单!