I am trying to render an html response from an API in React.
I think the problem can be that I am not handling the async fetch correctly. I am not sure if the html string I am fetching from the BE is a promise or a string. When I log it below I get a Promise
I used the code from this answer to use dangerouslySetInnerHTML
to render the html though I am not sure if this is the right way to render a complete page. The backendHtmlString is a complete page that I would just like to add to React.
App.js
- React code to render 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();