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

142
Visualizações
Unable to dynamically render custom react components properly after fetching from backend

I am trying to fetch data from the backend and then render a custom component with props as the data fetched from the backend. Here's a snippet:

import { useEffect } from 'react';
import axios from 'axios';
import Card from './Card';

export default function DesignList() {
    useEffect(() => {
        async function populate() {
            const response = await axios({
                method: 'POST',
                baseURL: 'http://localhost:3001',
                url: '/recommend',
                headers: { 'Content-Type': 'application/json' },
            });

            response.data.forEach(recommendation => {
                document.getElementById('design-list').append(<Card
                    title={recommendation.title}
            });

        }
        populate();
    })

    return (
        <div id='design-list' />
    )
}

I also tried to use React.createElement(<Card .../>), pushed it in an array and tried to render it but I got the same output as described below.

This gives an output on the screen but not as a HTML component. The output for each component is [object Object]

output

How do I render the components dynamically after fetching the props data from backend?

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

0

Use state for showing recommendations, not dom append.

import { useEffect, useState } from "react";
import axios from "axios";
import Card from "./Card";

export default function DesignList() {
  const [recommendations, setRecommendations] = useState([]);
  useEffect(() => {
    async function populate() {
      const response = await axios({
        method: "POST",
        baseURL: "http://localhost:3001",
        url: "/recommend",
        headers: { "Content-Type": "application/json" },
      });
      setRecommendations(response.data);
    }
    populate();
  });

  return (
      <div id="design-list">
        {recommendations.map((item) => (
          <Card title={item.title} />
        ))}
      </div>
  );
}
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