Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

75
Visualizações
state display is not updated if data is removed

I have an array of files, that you can fill via a type="file" tag. Data is then displayed to the user, where the user himself is able to click a little cross at the right side of the uploaded files name.

Adding data to the array state works with a perfect update. But if data is removed, and I can see the state being an empty array by logging its value to the console, the displayed data is not updated.

const [pfps, setPfps] = useState([])


function handleSetPfps(e) {
    setPfps([...pfps, e.target.files[0]])
}

async function removeFile(i) {
    const newPfps = pfps
    await newPfps.splice(i, 1)
    setPfps(newPfps)
}

And this is the function that shows the files names, that still shows the files names even after they have been removed from the state.

{pfps?.map((pfp, index) => (
    <div className='bg-slate-200 p-1 m-1 block'>
        {short(pfp.name)}
        <XIcon className='w-4 h-4 inline ml-3 mr-1' onClick={async () => await removeFile(index)} />
    </div>
))}

How can I solve this error and see the actual change, when I remove things from the state?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda