What I want to do is to fetch a website, then convert it into HTML, but not put the entire HTML into the React DOM, first select an element (and obviously it's children), then put this specific element into the React DOM. What I want is something like this:
convertHtml = () => {
fetch(url)
.then(res => res.text())
.then(htmlString => {
/*for example the htmlString looks like this:
"<html>
<body>
<div>
<p id="elementWhatIWant">
I want to earn this element (not the text inside)
</p>
</div>
</body>
</html>"
*/
//what I want:
return stringToHtml(htmlString).getElementById("elementWhatIWant")
})
}
render = () => <div className="App">
{this.convertHtml()}
</div>
I did some research, but as I saw, there's only one way to get an element in React, and it's the ref. Because it's a string, we can't put ref on any element directly. The thing what I thought is that we can parse the string, and
- search the
elementWhatIWant
element, and put the ref on it as a string, or - search the
elementWhatIWant
element, search it's closing tag, and return just this part of the string
Neither of them seems like a good approach, that's why I'm asking: What is the best way to earn what I want?