I have the following React component below. The code is successful in sending my file to the back-end as desired. However, I would like to set set the state back to null after the submit button is clicked. What would be the best way to do so here?
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>
)}
}