If within a React functional component I create a DOM node like this
const divElement = document.createElement("div");
What would be the best way to inject that into what is being rendered by the component?
For instance
const MyComponent: React.FC<Props> = () => {
const divElement = document.createElement("div");
return (
<div className="my-component">{divElement}</div>
);
}
This example doesn't work. It throws an error saying :
Objects are not valid as a React child
But it does reflect the type of solution I'm hoping to achieve, where I can just render the element directly into the content somehow.
The only way I've found to get this to work so far is to use a ref on my component's container div and call appenchChild(divElement) on the ref. But this is not an ideal solution for me.
I also realize that there are much easier ways to achieve what my example is doing. But this question stems from a thirdParty lib I'm using that returns a DOM node and I'm trying to figure out how to inject it.