I'm iterating through a collection in an event handler and run the function setInStateAsBase64
on each item to convert it and set it to state.
When the iteration is done I want to get all items set to state, so I can upload them. But I can't (despite them being successfully set to state), because of some asynchronicity going on.
In what way should I restructure the code in this React context to get hold hold of these values (to be able to upload them).
setInStateAsBase64(file) {
var self = this;
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
const newTempFiles = [...self.state.tempFiles, reader.result];
self.setState({
tempFiles: newTempFiles
});
};
}
handleDrop(acceptedFiles, rejected) {
acceptedFiles.map((file) => {
this.setInStateAsBase64(file)
})
console.log(22, this.state.tempFiles.length)
// returns 0 items
}