setState does not work onDrop
See original GitHub issueFor some reason this.setState is not working properly when onDrop is called. Here is my code:
class App extends Component {
constructor(props){
super(props);
this.state= { files: [] };
}
onDrop(accepted, rejected){
this.setState({files: accepted});
console.log('accepted: '+this.state.files);
console.log('rejected: '+rejected);
}
render() {
return (
<div className="App">
<Dropzone onDrop={this.onDrop}>
<div>Upload some stuff here </div>
</Dropzone>
{this.state.files.length > 0 ? <div>
<h2>Uploading {this.state.files.length} files...</h2>
<div>{this.state.files.map((file) => <img src={file.preview} /> )}</div>
</div> : null}
</div>
);
}
}
1st time i add a file onDrop() outputs this.state.files as undefined .
the 2nd time i add a file, onDrop() outputs this.state.files as the previous file uploaded.
If i log output this.state.files from another function, this.state.files is ALWAYS empty, even though onDrop() calls the previous result.
This sounds like a bug to me. I’ve tried this.onDrop.bind(this). This also will never update the actual state.
it seems that the state inside onDrop is separate to the state inside the main app.
I know that setState is asynchronous. But that’s meaningless when it’s not updating the actual state.
Very frustrating.
Issue Analytics
- State:
- Created 7 years ago
- Comments:9
Top Results From Across the Web
React Drag and Drop - onDrop not called if setState called in ...
I'm providing my solution in case anyone else runs into this problem. The best work around I could come up with was to...
Read more >React drag and drop - Medium
So recently, I found myself working on a project that required me to implement a drag and drop feature. With no prior knowledge...
Read more >[Solved]-React setState is not a function upon adding JSON?
Coding example for the question React setState is not a function upon adding JSON? ... function FileUploader(props) { // Define an onDrop function...
Read more >Reactjs – Material-UI LinearProgress bar not working – iTecNote
...otherwise, all your "this" references inside of onDrop() won't be correct, so "this.state" and "this.setState" would fail. But also, ...
Read more >React.js: implement the drag and drop feature without using ...
So, easy even your dog can drag it :)Let's first see the result of ... Let's open up the render method and add...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

This is because you expect setListOfFiles({files}) to be syncronous. But it is not. You can use
useEffectto get the dropped filesThanks @okonet but this isn’t working.
The states are being passed through into
onDrop()with bind. ButsetStatedoes not update the actual state.this.state.filesinrender()is still unchanged and still remains empty no matter how many times i add a file.But weirdly
this.state.filesshow the previous file data INSIDEonDrop()function only