How can I transfer individual items from one array into another using react?
I have an array of items in state I created, but I need to send them one by one into another state. What's the best way to do this? I tried callback functions and I'd rather not use setTimeone.
state = { imageChosen: [] };
// Choose multiple images to upload
fileSelectedHandler = event => {
let tmp = [];
for (let i = 0; i < event.target.files.length; i++) {
tmp.push(event.target.files[i])
}
this.setState({ imageChosen: tmp }, () => this.uploadMedia());
}
// Add chosen images to the media chosen array
uploadMedia = () => {
for (let i = 0; i < this.state.imageChosen.length; i++) {
this.setState({
mediaChosenArr: [...this.state.mediaChosenArr, this.state.imageChosen[i]],
displayMediaChosenArr: [...this.state.displayMediaChosenArr, URL.createObjectURL(this.state.imageChosen[i])],
imageChosen: [],
});
}
document.getElementById("mediaPlaceholder").value = '';
};