0

I am trying to render an html component that has a comment it so that the comment shows up in the DOM as comment.

I currently have

<head>
    <title>{props.pageTitle}</title>
</head>

But I would like to have

<head>
    <title>{props.pageTitle}</title>

    <!-- Some information here --> 

</head>

And I would like to avoid using the following:

dangerouslySetHtml
element.innerHTML = "...";
element.outerHTML = "...";
es3735746
  • 841
  • 3
  • 16
  • 40

1 Answers1

2

Once the component mounted, you could retrieve the current DOM element and, then apply some DOM operations to place the HTML comment wherever you need. You can find below an example of a component wrapped by HTML comments.

componentDidMount() {       
    // Get current node
    let currentNode = ReactDOM.findDOMNode(this);
    // Get parent node
    let parentNode = currentNode.parentNode;
    // Create the comment to insert before
    let commentBefore = document.createComment("Begin Tag");
    parentNode.insertBefore(commentBefore, currentNode);
    // Create the comment to insert after
    let commentAfter = document.createComment("End Tag");
    parentNode.insertBefore(commentAfter, currentNode.nextSibling);
}
Adrien M.
  • 29
  • 3