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

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

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 Relatório

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 Relatório

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