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

135
Vistas
React component won't re-render on state change

Here is my code. The imageChunks state variable is not updating correctly. Selecting an image returns an empty array. Selecting another image returns an array of the last image's base64 chunks. It seems that setImageChunks() is not triggering a re-render. What is causing this? Any help would be appreciated.

const [ imageChunks, setImageChunks ] = useState([])
    
const handleImageChange = async (e) => {    
            const files = e.target.files
    
            // Break image base64 string into chunks for database upload, store each in object with series (filename) and part
            const getChunks = (filename, baseStr, chunkSize) => {
                const str = baseStr.split("")
                const chunks = []
                let part = 0
                let count = str.length
                let start = 0
                while (count){
                    part++
                    start = str.length - count
                    const allowedCount = count < chunkSize ? count : chunkSize
                    chunks.push({
                        series: filename,
                        part: part,
                        data: str.slice(start, start + allowedCount).join("")
                    })
                    count -= allowedCount
                }
                
                return chunks 
            }
    
            // empty imageChunks array to prepare for new file selection
            const fileChunks = []
    
            for (let i = 0; i < files.length; i++){
                // Push file name to product object imageFiles array  
                const fileNames =  formValues.imageFiles
                fileNames.push(files[i].name)
                setFormValues({...formValues, imageFiles: fileNames})
    
                // get array of file chunk objects, push to imageChunks array
                const file = await new Response(files[i]).arrayBuffer()
                const baseString = Buffer.from(file).toString('base64')
                const chunks = getChunks(files[i].name, baseString, 10000)
    
                fileChunks.push(chunks)
            }
    
            setImageChunks(null)
            setImageChunks([...fileChunks])
            console.log(imageChunks)
        }
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