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

225
Visualizações
Why my array resets itself when I leave my function?

I come here after two whole days without success. I want to increment my array with the pictures selected by the User, for return to my database a list of uri. But when the user choosed the first picture and re-open the android navigator for take a new picture, I saw the array doesn't increment. It takes the first value, and when I leave the selectImage() method it is back to empty.

Here is my main function :

EDIT :

const arrayImage = [];

const selectImage = () => {
    const options = {
      maxWidth: 2000,
      maxHeight: 2000,
      storageOptions: {
        skipBackup: true,
        path: 'images'
      }
    };
    launchImageLibrary(options, response => {
        if (response.didCancel) {
            console.log('User cancelled image picker');
          } else if (response.error) {
            console.log('ImagePicker Error: ', response.error);
          } else if (response.customButton) {
            console.log('User tapped custom button: ', response.customButton);
          } else {
             async(response);   
            }
    })

    function async(param) {
      setTimeout(function() {
          const source = { uri: param.assets[0].uri }
          array ?
          setImage(source)
         : arrayImage.push(source)
         console.log(arrayImage);  
      }, 2000);
  }

    console.log(arrayImage)
  };

I tried with promises, changing my function in async, with a SetTimeOut but I guess the probleme is not with the Asynchronous. Someone for help ? Thanks a lot.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Use useState for your array to keep state even if you leave the screen:

import { useState } from "react";

const yourComponent = () => {
  const [array, setArray] = useState([]);
}

And in your function instead of array.push(source) you use it like that:

const newArray = [...array, source];
setArray(newArray);

And this should keep your result as I think the problem comes from the array and not the asynchronous function.

about 4 years ago · Juan Pablo Isaza 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