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

222
Visualizações
How can I fix the scope of a array variable so that I can map through it in React?

I have the following code:

const getNFTs = async (address: string) => {
... // hidden for brevity

    //Nested code
    const promises = Array.from({length: output.toNumber()}).map() => {
        ... // hidden for brevity
    }

const promiseArray = await Promise.all(promises); //returns object
const imageArray = Object.values(promiseArray); //converts to array
}

If I was to console.log imageArray I am given an array like this:

['https://imagepath.com/image1.png','https://imagepath.com/image2.png','https://imagepath.com/image3.png',]

I want to use imageArray to map through the array and render a grid of images on the UI. But when I try to map through this, I get an error that imageArray is not defined most likely due to scoping issues.

Do I write the map function (with the required HTML/JSX markup) within the getNFTs async function? Or is there a better way to access the array to generate the list of images on the UI?

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

0

promiseArray should already be an array of image URLs, Object.values will effectively yield the same value. Recall that an Array is just a special object of associative key-value pairs, i.e. the keys are the array indices and the values are the stored array values. If the returned resolved value isn't the image URLs then you can likely "unpack" them higher up in the Promise chain when processing the asynchronous requests.

You can save this array to the outer scope by adding some React state to hold this array value, and enqueue a state update to save it and trigger a rerender.

Example:

const [imageArray, setImageArray] = React.useState([]);

...

const getNFTs = async (address: string) => {
  ... // hidden for brevity

  //Nested code
  const promises = Array.from({ length: output.toNumber() }).map() => {
    ... // hidden for brevity
  }

  const imageArray = await Promise.all(promises);
  setImageArray(imageArray);
};

...

Later you can reference the imageArray state and render it as necessary.

imageArray.map(.....)
about 4 years ago · Santiago Trujillo 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