I've started with learning React JS and my problem is that; I want to select multiple file from file dialog and then I want to display them on the screen with their name.
<RaisedButton className="raisedBtn" label="Video Ekle" onClick={this.openFileDialog}>
<input
ref={(input) => { this.textInput = input; }}
style={{"display" : "none"}}
type="file" multiple
onChange={this.handleFileChange}
>
</input>
</RaisedButton>
There is okay.
openFileDialog(){
ReactDOM.findDOMNode(this.textInput).click();
}
There is okay again.
The problem is there:
handleFileChange(e){
var files = e.target.files;
for (var i = 0; i < files.length; i++) {
var file = files[i];
let reader = new FileReader();
var fileName = file.name;
reader.onload = (e) => {
tilesData.push({img: e.target.result, title: fileName})
this.setState({image: e.target.result, title: fileName});
};
reader.readAsDataURL(file);
}
}
All images name are the same (all of them have the last selected file's name). I think reader.onload is running asynchronous so the for loop go on but reader.onload is not finish the running? How can I fix it?
Thanks in your advance.