facebook/react

Bug: Request for JSX Support for HTML Comments () in React

robertpanvip opened this issue · 1 comments

Description

Currently, React does not support rendering HTML comments () directly in JSX. This limitation makes it challenging to implement certain use cases.

For instance, in React 19, findDOMNode has been deprecated, but in some scenarios, we still need similar functionality, such as retrieving a component’s DOM reference or marking specific nodes. A potential solution could be using comment nodes () as placeholders to enable subsequent DOM operations involving these nodes.

Feature Request

We hope React could support rendering comment nodes directly in JSX, such as:

jsx

<div>  
  <!-- This is a comment node -->  
</div>  

Rendering as:

html

<div>  
  <!-- This is a comment node -->  
</div>  

Why This Is Needed

Enhance DOM Manipulation Capabilities
Comment nodes can serve as lightweight markers for advanced DOM operations, such as dynamically inserting content or replacing deprecated findDOMNode functionality.

Flexibility and Compatibility
In some legacy systems, comment nodes are used to identify specific logic. Supporting this feature would allow developers to migrate old projects more easily or implement more complex functionality.

Improve Developer Experience
Allowing HTML comments in JSX can make the code more readable while helping developers document and debug specific logic directly within their JSX code.

Possible Alternatives

If supporting directly is not feasible, React could introduce a component like :
jsx

<div>  
  <Comment>This is a comment node</Comment>  
</div> 

Rendering as:

html

<div>  
  <!-- This is a comment node -->  
</div>  

Conclusion

We kindly request the team to consider this feature.

I would like to work on this feature. kindly assign it to me!