Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

386
Views
Representación de la respuesta html de la API en React

Estoy tratando de generar una respuesta html desde una API en React.

Creo que el problema puede ser que no estoy manejando correctamente la recuperación asíncrona. No estoy seguro de si la cadena html que obtengo del BE es una promesa o una cadena. Cuando lo registro a continuación, recibo una Promise

Usé el código de esta respuesta para usar dangerouslySetInnerHTML SetInnerHTML para representar el html, aunque no estoy seguro de si esta es la forma correcta de representar una página completa. El backendHtmlString es una página completa que me gustaría agregar a React.

App.js - Código de reacción para renderizar html:

 async function createMarkup() { let response; response = await fetch(`http://localhost:8000/backed_api/html_response/?user_email=chriss%40comtura.ai`) const backendHtmlString = response.text() console.log(backendHtmlString) return {__html: backendHtmlString}; } function MyComponent() { return <div dangerouslySetInnerHTML={createMarkup()} />; } function App() { return ( <div className="App"> <MyComponent/> </div> ); } export default App;

Index.js

 import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; import reportWebVitals from './reportWebVitals'; ReactDOM.render( <React.StrictMode> <App /> </React.StrictMode>, document.getElementById('root') ); // If you want to start measuring performance in your app, pass a function // to log results (for example: reportWebVitals(console.log)) // or send to an analytics endpoint. reportWebVitals();
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

¡Las funciones asíncronas siempre devuelven una Promise ! Asegúrese de resolverlo para obtener los datos.

Consulte: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

Dado que los datos se obtienen del backend, puede llevar algún tiempo. Puede usar useEffect para realizar la solicitud y establecer el valor que obtiene del servidor usando useState .

 function MyComponent() { const [html, setHTML] = useState({__html: ""}); useEffect(() => { async function createMarkup() { let response; response = await fetch(`http://localhost:8000/backed_api/html_response/?user_email=chriss%40comtura.ai`) const backendHtmlString = await response.text() console.log(backendHtmlString) return {__html: backendHtmlString}; } createMarkup().then(result => setHTML(result)); }, []); return <div dangerouslySetInnerHTML={html} />; }

Además, echa un vistazo a este escenario . Podría ser otro caso similar al tuyo.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!