I am trying to transform a class component in a functional component but I have the feeling that the useState(in a functional component) and the setState(from a class component) are not really working the same way.
Here is the code in the class component that is working well:
// Load images
for (var i = 1; i <= imagesCount; i++) {
const image = await tipMyMeme.methods.images(i).call()
this.setState({
images: [...this.state.images, image]
})
}
this.setState({ loading: false})
And here the same code in a functional component that is not working:
for (var i = 1; i <= imagesCount; i++) {
const image = await tipMyMeme.methods.images(i).call()
setImages(images => [...images, image]);
}
console.log("images",images)
if (images.length==imagesCount)
{
setLoading(false)
}
In the console.log(images), I am noticing that the array is staying empty. Is there here a real difference in the way a class and a functional component work?
What I am trying to achieve is to load a list of images and send them as props in the sub component. The process of loading is working well, for exemple, if I put a console log under const image, I can see each image. What is not working I feel is the process of creating a state array of images with setImages.