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

112
Visualizações
Fetch multiple URL and setting the results in a state

I have to fetch each url within the ProductsUrl array and set the response that the fetch returns within the Products state, so that later in the render, render each product with a products.map

I've tried promise.all, axios.all, async await... thousands of ways and I can't figure it out.

Also have tried with another examples here in stack but i could not find the answer

import React, { useState, useEffect } from 'react'
import Card from './Card'

function Main() {
    const productsUrl = [
        'https://api.fasdfds.com/items/MLA884216659',
        'https://api.fdsfsd.com/items/MLA646715969',
        'https://api.fdsfsf.com/items/MLA906258237'
    ]

    const [productos, setProductos] = useState([])

    useEffect(() => {
        productsUrl.forEach(url => fetch(url))
            .then(res => res.json())
            .then(data => setProductos([...productos, data]))
    }, [])
    return (
        <main>
            {productos ? productos.map(producto =>
                <Card
                    key={producto.id}
                    src={producto.thumbnail}
                    title={producto.title}
                    prevPrice={producto.original_price}
                    newPrice={producto.price}
                    discount={producto.original_price / producto.price}
                />

            )
                :
                <h1>loading</h1>
            }



        </main>
    )
}

export default Main
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