Using JavaScript Comments in JSX

You can place JavaScript comments anywhere in React/JSX you want except locations where JSX might expect a React child node. In this situation you’ll have to escape the comment using { } so that JSX knows to pass that on as actual JavaScript.

Examine the code below, make sure you understand where you’ll have to tell JSX to pass along a JS comment so a child React node is not created.

  1. var reactNode = <div /*comment*/>{/* use {'{}'} here to comment*/}</div>;

In the above code if I had not wrapped the comment inside of the <div> with { } brackets then Babel would have converted the comment to a React text node. The outcome, unintentionally, without the { } would be:

  1. var reactNode = React.createElement(
  2. "div",
  3. null,
  4. "/* use ",
  5. "{}",
  6. " here to comment*/"
  7. );

Which would result in the following HTML that would have unintended text nodes.

  1. <div data-reactid=".0">
  2. <span data-reactid=".0.0">/* use </span>
  3. <span data-reactid=".0.1">{}</span>
  4. <span data-reactid=".0.2"> here to comment*/</span>
  5. </div>