我在下面有以下React组件。该代码成功将我的文件发送到所需的后端。但是,我想在单击“提交”按钮后将状态设置回null。这样做的最佳方法是什么?
state = {
selectedFile: null
}
handleInputChange = event => {
this.setState({
selectedFile: event.target.files[0]
})
}
fileUpload = () => {
var fd = new FormData();
fd.append('form', this.state.selectedFile, this.state.selectedFile.name);
fetch('/upload', {method: 'POST', credentials: 'include', body: fd})
.then(res => {
console.log(res);
})
this.setState({ selectedFile: Null })
}
render() {return (
<Form>
<h2>Request Form Upload</h2>
<Form>
<Form.Input label="Attach a file" name='Form' type="file" onChange={this.handleInputChange} autoFocus />
<Form.Button onClick={this.fileUpload}>Upload</Form.Button>
</Form>
</Form>
)}
}