question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

setState does not work onDrop

See original GitHub issue

For 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:closed
  • Created 7 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
itsashis4ucommented, Mar 19, 2020

This is because you expect setListOfFiles({files}) to be syncronous. But it is not. You can use useEffect to get the dropped files

1reaction
abbasfreestylecommented, Feb 17, 2017

Thanks @okonet but this isn’t working.

The states are being passed through into onDrop() with bind. But setState does not update the actual state.

this.state.files in render() is still unchanged and still remains empty no matter how many times i add a file.

But weirdly this.state.files show the previous file data INSIDE onDrop() function only

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found