I am trying to submit a file and text input to backend with POST request, the code looks like this,
state={
text:'',
file:''
}
handleTextChange = (e) => {
this.setState({[e.target.name]:e.target.value})
}
handleFileChange = (e) => {
this.setState({file:e.target.files[0]})
}
handleSubmit = (e) => {
const {text,file} = this.state
//POST request with text and file
}
But the file is always empty, If I console.log(e.target.files[0]), it shows a FileList in console but somehow it is not updated in state
I am quite new in react, your help is very much appreciated, thanks
EDIT: the form looks like this
<form action="" className="w-100" onSubmit={(e) => this.handleSubmit(e)}>
<div className="pt-1 pb-3">
<TextAreaAutosize
className="form-control"
placeholder="write some text here"
minRows="4"
name="text"
onChange={(e) => this.handleChange(e)}
/>
</div>
<div className="w-100 d-flex justify-content-between align-items-center">
<div>
<input
type="file"
className="form-control-file"
name="file"
onChange={(e) => this.handleFileChange(e)}
/>
</div>
<div>
<button type="submit" className="btn btn-primary">
Post
</button>
</div>
</div>
</form>