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

250
Visualizações
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 Respostas
Responde à pergunta

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 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