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

231
Vistas
How to populate a div from the data from fetch

I am getting my output in a react app at this.state but I want to bring this outside and populate div so that I can show it at the frontend. How can I do it?

 fetch("https://flask-api-test1.herokuapp.com/predict", {
      method: "post",
      body: formData,
    })
      .then((res) => res.json())
      .then((result) => {
        console.log(state);
      })
      .catch((err) => {
        console.log(err);
      });

output from the api

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

0

Your example is a bit incomplete. Anyways, here is how I would do that if the returned data was an array https://codesandbox.io/s/mutable-leftpad-o3v30?file=/src/App.js

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you can use hooks then the simplest solution is useState

import {useState} from 'react'  

declare

const [newState, setNewState] = useState()

then you try

 fetch("https://flask-api-test1.herokuapp.com/predict", {
      method: "post",
      body: formData,
    })
      .then((res) => res.json())
      .then((result) => {
        setNewState(result)
        console.log(JSON.stringify(result));
      })
      .catch((err) => {
        console.log(err);
      });

and you can use ready-made 'newState' for div in jsx like

<div>
 {newState?.keyDataYouNeed}
</div>
about 4 years ago · Juan Pablo Isaza Denunciar

0

never do this.state and assign values directly, as its immutable, what you can do is ,

fetch("https://flask-api-test1.herokuapp.com/predict", {
  method: "post",
  body: formData,
})
  .then((res) => res.json())
  .then((result) => {
    this.setState({data:result});
    console.log(result);
  })
  .catch((err) => {
    console.log(err);
  });
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