Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

239
Vistas
ReactJS, new axios call at every render of the component

I'm trying to dynamically render some components mapping an array of objects from an API call. I can't wrap my head around the syntax of this one.

I have to fetch from this url:

https://fakestoreapi.com/products

And for each object in the array I have to render Product.js inside ProductContainer.js.

ProductContainer.js:

import axios from "axios";
import React, { useState, useEffect } from "react";
import "../styles/ProductContainer.scss";
import Product from "./Product";

const ProductContainer = () => {
  const [productArray, setProductArray] = useState("");
  const [imgUrl, setImgUrl] = useState("");
  const [nameUrl, setNameUrl] = useState("");
  const [priceUrl, setPriceUrl] = useState("");

  useEffect(() => {
    axios.get("https://fakestoreapi.com/products").then((res) => {
      setProductArray(res.data);
      setImgUrl(res.data.image);
      setNameUrl(res.data.title);
      setPriceUrl(res.data.price);
    });
  }, []);

  return (
    <div className="product-container">
      {productArray.map((e) => {
        <Product imgUrl={imgUrl} nameUrl={nameUrl} priceUrl={priceUrl} />;
      })}
    </div>
  );
};

export default ProductContainer;

Thank you!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The image, title and price are properties of each product, not of the whole response from the API.

Also productArray should be initialized to an array and not a string.

So

import axios from "axios";
import React, { useState, useEffect } from "react";
import "../styles/ProductContainer.scss";
import Product from "./Product";

const ProductContainer = () => {
  const [productArray, setProductArray] = useState([]);

  useEffect(() => {
    axios.get("https://fakestoreapi.com/products").then((res) => {
      setProductArray(res.data);
    });
  }, []);

  return (
    <div className="product-container">
      {productArray.map((product) => {
        <Product imgUrl={product.image} name={product.name} price={product.price} />;
      })}
    </div>
  );
};

export default ProductContainer;
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda