admin管理员组文章数量:1431997
I am trying to setState for a file object that i am passing between modals before i upload it to server. Following is the code that i am trying to implement with.
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 },
}
));
}
I am trying to setState for a file object that i am passing between modals before i upload it to server. Following is the code that i am trying to implement with.
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 },
}
));
}
Share
Improve this question
asked Mar 16, 2017 at 4:02
Deep VoraDeep Vora
3281 gold badge3 silver badges13 bronze badges
1 Answer
Reset to default 2Set state as:
const state = {
selectedDocument: {
file: null,
},
selectedFile: null,
};
Then, set state as:
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],
});
As easy as that!
本文标签: javascriptHow to save an file object to state in ReactStack Overflow
版权声明:本文标题:javascript - How to save an file object to state in React? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745584254a2664799.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论