I'm posting this because I search on the forum but didn't find the answer.
I have a front app which make a request, and the server is responding an html which I want to display on the client.
client:
function createElementFromHTML(htmlString) {
var template = document.createElement('template');
template.innerHTML = htmlString.trim();
return template.content.firstChild;
}
callAPI(jsonData) {
fetch('http://localhost:9000/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: jsonData,
})
.then(async response => response.text())
.then(async string => createElementFromHTML(string))
.then(async html => ReactDOM.render(html, document.getElementById("root")));
}
server node js:
router.post('/', function (req, res, next) {
login(req.body.email, req.body.password)
.then(res.send('<h2>You are logged in</h2>'));
})
module.exports = router;
The problem was that the server response is a string and I need to convert it to a DOM element in order to render it, so I check this post Creating a new DOM element from an HTML string using built-in DOM methods or Prototype, but I have a new error message:
Objects are not valid as a React child (found: [object HTMLHeadingElement]). If you meant to render a collection of children, use an array instead.
It comes from the line :
.then(async html => ReactDOM.render(html, document.getElementById("root")));
But I don't know how to solve it, Thanks in advance for your help !