I am hitting an API endpoint, which returns the HTML for a blog post as a string. So lots of <p>
tags, <img>
, <span>
, and <div>
tags in the response string, with a non-inconsequential amount of element nesting.
in a react app, I want to parse this HTML string, and replace some of the HTML elements (images, paragraphs) with custom react components ( ie wrap an image tag in a lazy-load react component).
what is a good / clean way to do this? trying to parse HTML with regex seems to be an antipattern, and I'm not sure what other possible options there are. parse the string into traditional dom nodes, and then iterate through the collection?
currently I just render the html-as-string inside a react component using dangerouslySetInnerHTML
.
What is a reliable, non-regex way to wrap html-as-a-string entities with React components, while maintaining the original order of the nodes (ie in a blog post)?