I'm making a file input to select multiple images that can be pre-visualized.
To handle multiple images I put them in an array of the component's state, and map the pre-visualized images.
But when I select the images from the input and set the state with this.setState({imgArray: newArray})
, this.state.array.map(image=><img src={image}/>)
doesn't re-render the selected images.
Code:
export default class posts extends Component {
state = {
fotos: ["/iconos/img.svg"] // If there are not img selected, it renders one image icon
}
onUploadVarious = e => {
let newArray = []
Object.values(e.target.files).map(file => {
let nuevo = new FileReader()
nuevo.onload = event=> newArray.push(event.target.result)
nuevo.readAsDataURL(file)}
)
this.setState({fotos: newArray}) // the state is set correctly
}
}
Render:
<div className=" border rounded" style={{"height":"30%", "overflow":"auto"}}>
{this.state.fotos.map(foto =>
<img src={foto||"/iconos/img.svg"}
id="arch-preview"/>)} // it doesn't re-render when fotos state is changed
</div>
// input
<div className="mt-auto">
<input multiple="multiple" type="file"
onChange={this.onUploadVarious}
className="form-control-file" name="file" />
</div>