ReactJS - How to use comments?

后端 未结 13 1769
情深已故
情深已故 2020-12-04 08:17

How can I use comments inside the render method in a React component?

I have the following component:

\'use strict\';
          


        
13条回答
  •  暖寄归人
    2020-12-04 08:26

    To summarize, JSX doesn't support comments, either html-like or js-like:

    /* This will be rendered as text */ // as well as this

    and the only way to add comments "in" JSX is actually to escape into JS and comment in there:

    {/* This won't be rendered */} {// just be sure that your closing bracket is out of comment }

    if you don't want to make some nonsense like

    actually, there are other stupid ways to add "comments" but cluttering your DOM is not a good idea

    Finally, if you do want to create a comment node via React, you have to go much fancier, check out this answer.

提交回复
热议问题