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

129
Visualizações
How to append to a previous state array while executing an arrow function inside setState?

I am trying to append file(s) to previous file state when drag and dropping image(s) to React-Dropzone library.

 const [files, setFiles] = useState([])

...
const { 
        getRootProps, 
        getInputProps,
        isFocused,
        isDragAccept,
        isDragReject, 
    } = useDropzone({
        accept: "image/*",
        onDrop: (acceptedFiles) => {
           setFiles(acceptedFiles.map((file) => Object.assign(file, {
                preview: URL.createObjectURL(file)
            })))
        }
    })

In the "onDrop", I map the accepted files and append the new array using setFiles. However, using this resets the whole array and previous images are lost. I want to append the images. I've tried doing:

setFiles([...files, acceptedFiles.map((file) => Object.assign(file, {
                preview: URL.createObjectURL(file)
            }))])

But this returns nothing and gives me no images at all.

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

When appending to an array in React state it can be very convenient to use the functional update form of useState - which is just a fancy way of saying you'll write an arrow function like this:

   setFiles((oldArrayofFiles) => {
     return [...oldArrayOfFiles, newFile];
   })

This also prevents the problem that I think you're seeing, where because you supply onDrop as a function, you're closing over the files when it's still an empty array. So in your case (and forgive me, I haven't tested this with React-Dropzone) this should work:

useDropzone({
  accept: "image/*",
  onDrop: (acceptedFiles) => {
    setFiles((previousFiles) => {
      const newFiles = acceptedFiles.map((file) => Object.assign(file, {
        preview: URL.createObjectURL(file)
      }));

      return [...previousFiles, ...newFiles];
    });
  });

I might have messed up a bracket or three ... but you get the idea :-)

about 4 years ago · Santiago Gelvez Relatório
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