I'm making a Gatsby site, and after running some GraphQL queries, I pass some HTML strings into React components. I've been able to render these with dangerouslySetInnerHtml, html-react-parser, etc. However, I'd like to also just write component tags within the original Markdown and render those as components.
A simple example of this would be
import React from "react";
export default function App() {
const RedDiv = () => {
return <div style={{ color: "red" }}>This is a red div</div>;
};
const StringRedDiv = "<div style={{color: 'red'}}>This is a red div</div>";
return (
<div className="App">
<RedDiv />
<div dangerouslySetInnerHTML={{ __html: StringRedDiv }} />
</div>
);
}
Obviously, we don't need to use dangerouslySetInnerHtml here, but I'd like to achieve the desired effect (in this case having both divs have red text) using a method which takes an HTML string and transforms it into React.