5

I am using ReactJS and I have a requirement to add visible HTML comment (in html source) in JSX but I am not sure how to do that.

This comment is needed by a third party vendor to do some processing on the html between the comments.

<!-- trans_disable_start -->
<div> testing </div>
<!-- trans_disable_end-->

I tried the below options but the comment is not visible in html source.

{/* <!-- trans_disable_start -->*/}

Tried using this library - react-render-html

{renderHTML('<!-- trans_disable_start -->')}

Any ideas / thoughts is appreciated.

Update 1:

I tried to use the below JS library but that doesnt work since I am using the server side rendering and I see the below value in page source on initial load (Rather than comment)

https://github.com/optimalisatie/react-jsx-html-comments

<react-comment data-reactid="18">trans_disable_start</react-comment>
Learner
  • 2,203
  • 6
  • 42
  • 79

1 Answers1

7

I think dangerouslySetInnerHTML can work for you.

Example

<div dangerouslySetInnerHTML={{__html: "<!-- some comment -->"}} />
bennygenel
  • 22,268
  • 6
  • 60
  • 76