Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

89
Vistas
updating a useState array in a functional component in react

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.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda