I am trying to upload a file in reactjs, I want to display in the console, but when I click on upload button it gives me
"TypeError: Cannot read property 'state' of undefined"
Here is my code:
constructor(props) {
super(props);
this.state = {
selectedFile: null
};
this.fileSelectedHandler = this.fileSelectedHandler.bind(this);
}
fileSelectedHandler(event) {
this.setState({
selectedFile: event.target.files[0]
})
}
handleUpload() {
console.log(this.state.selectedFile)
}
render() {
return (
<div class="group">
<input type="file" name="file" id="file" onChange={this.fileSelectedHandler} />
<button onClick={this.handleUpload}> Upload </button>
</div>
)
}
I am getting the error at: console.log(this.state.selectedFile)