How to use comments in React

ReactjsReact Native

Reactjs Problem Overview


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

I have the following component:

'use strict';
 var React = require('react'),
   Button = require('./button'),
   UnorderedList = require('./unordered-list');

class Dropdown extends React.Component{
  constructor(props) {
    super(props);
  }
  handleClick() {
    alert('I am click here');
  }

  render() {
    return (
      <div className="dropdown">
        // whenClicked is a property not an event, per se.
        <Button whenClicked={this.handleClick} className="btn-default" title={this.props.title} subTitleClassName="caret"></Button>
        <UnorderedList />
      </div>
    )
  }
}

module.exports = Dropdown;

Enter image description here

My comments are showing up in the UI.

What would be the right approach to apply single and multiple line comments inside a render method of a component?

Reactjs Solutions


Solution 1 - Reactjs

Within the render method comments are allowed, but in order to use them within JSX, you have to wrap them in braces and use multi-line style comments.

<div className="dropdown">
    {/* whenClicked is a property not an event, per se. */}
    <Button whenClicked={this.handleClick} className="btn-default" title={this.props.title} subTitleClassName="caret"></Button>
    <UnorderedList />
</div>

You can read more about how comments work in JSX here.

Solution 2 - Reactjs

Here is another approach that allows you to use // to include comments:

return (
  <div>
    <div>
      {
        // Your comment goes in here.
      }
    </div>
    {
      // Note that comments using this style must be wrapped in curly braces!
    }
  </div>
);

The catch here is you cannot include a one-line comment using this approach. For example, this does not work:

{// your comment cannot be like this}

because the closing bracket } is considered to be part of the comment and is thus ignored, which throws an error.

Solution 3 - Reactjs

On the other hand, the following is a valid comment, pulled directly from a working application:

render () {
    return <DeleteResourceButton
            // Confirm
            onDelete = {this.onDelete.bind(this)}
            message = "This file will be deleted from the server."
           />
}

Apparantly, when inside the angle brackets of a JSX element, the // syntax is valid, but the {/**/} is invalid. The following breaks:

render () {
    return <DeleteResourceButton
            {/* Confirm */}
            onDelete = {this.onDelete.bind(this)}
            message = "This file will be deleted from the server."
           />
}

Solution 4 - Reactjs

Besides the other answers, it's also possible to use single line comments just before and after the JSX begines or ends. Here is a complete summary:

Valid

(
  // this is a valid comment
  <div>
    ...
  </div>
  // this is also a valid comment
  /* this is also valid */
)

If we were to use comments inside the JSX rendering logic:

(
  <div>
    {/* <h1>Valid comment</h1> */}
  </div>
)

When declaring props single line comments can be used:

(
  <div
    className="content" /* valid comment */
    onClick={() => {}} // valid comment
  >
    ...
  </div>
)

Invalid

When using single line or multiline comments inside the JSX without wrapping them in { }, the comment will be rendered to the UI:

(
  <div>
    // invalid comment, renders in the UI
  </div>
)

Solution 5 - Reactjs

According to the official site, these are the two ways:

<div>
  {/* Comment goes here */}
  Hello, {name}!
</div>

Second example:

<div>
    {/* It also works 
    for multi-line comments. */}
    Hello, {name}! 
</div>

Here is the reference: How can I write comments in JSX?

Solution 6 - Reactjs

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

<div>
    /* This will be rendered as text */
    // as well as this
    <!-- While this will cause compilation failure -->
</div>

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

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

if you don't want to make some nonsense like

<div style={{display:'none'}}>
    actually, there are other stupid ways to add "comments"
    but cluttering your DOM is not a good idea
</div>

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

Solution 7 - Reactjs

This is how.

> Valid:

...
render() {

  return (
    <p>
       {/* This is a comment, one line */}

       {// This is a block 
        // yoohoo
        // ...
       }

       {/* This is a block 
         yoohoo
         ...
         */
       }
    </p>
  )

}
...

> Invalid:

...
render() {

  return (
    <p>
       {// This is not a comment! oops! }
        
       {//
        Invalid comment
       //}
    </p>
  )

}
...

Solution 8 - Reactjs

Two ways to add comments in React Native

  1. // (double forward slash) is used to comment only single line in React Native code, but it can only be used outside of the render block. If you want to comment in a render block where we use JSX, you need to use the second method.

  2. If you want to comment something in JSX you need to use JavaScript comments inside of curly braces like {/* Comment here /}. It is a regular / Block comment */, but it needs to be wrapped in curly braces.

Shortcut keys for /* Block comments */:

  • Ctrl + / on Windows and Linux.

  • Cmd + / on macOS.

Solution 9 - Reactjs

{/*
   <Header />
   <Content />
   <MapList />
   <HelloWorld />
*/}

Solution 10 - Reactjs

{
    // Any valid JavaScript expression
}

If you wonder why it works, it's because everything that's inside curly braces { } is a JavaScript expression.

So this is fine as well:

{ /*
         Yet another JavaScript expression
*/ }

Solution 11 - Reactjs

JSX Comments Syntax: You can use

{/** 
  your comment 
  in multiple lines
  for documentation 
**/} 

or

{/* 
  your comment 
  in multiple lines
*/} 

for multiple lines comment. And also,

{ 
  //your comment 
} 

for single line comment.

> Note: The syntax: > > { //your comment } > > doesn't work. You need to type braces in new lines.

Curly braces are used to distinguish between JSX and JavaScript in a React component. Inside curly braces, we use JavaScript comment syntax.

Reference: click here

Solution 12 - Reactjs

According to React's Documentation, you can write comments in JSX like so:

One-line Comment:

<div>
  {/* Comment goes here */}
  Hello, {name}!
</div>

Multi-line Comments:

<div>
  {/* It also works 
  for multi-line comments. */}
  Hello, {name}! 
</div>

Solution 13 - Reactjs

JavaScript comments in JSX get parsed as text and show up in your application.

You can’t just use HTML comments inside of JSX because it treats them as DOM nodes:

render() {
  return (
    <div>
      <!-- This doesn't work! -->
    </div>
  )
}

JSX comments for single line and multiline comments follows the convention

Single line comment:

{/* A JSX comment */}

Multiline comments:

{/*
  Multi
  line
  comment
*/}

Solution 14 - Reactjs

Conditional rendering

This approach mentioned on the React docs will also work with nested /**/ comments, unlike the {/**/} approach, e.g.:

{false && <>
<div>
  Commented out.
  /* Anything goes. */
</div>
</>}

Full example:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello, World!</title>
<script src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/[email protected]/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
ReactDOM.render(
  <div>
    before
    {false && <>
    <div>
      Commented out.
      /* Anything goes. */
    </div>
    <div>
      Also commented out.
      /* Anything goes. */
    </div>
    </>}
    after
  </div>
  ,
  document.getElementById('root')
);
</script>
</body>
</html>

renders just beforeafter.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1177440View Question on Stackoverflow
Solution 1 - ReactjsHenrik AnderssonView Answer on Stackoverflow
Solution 2 - ReactjsanandharshanView Answer on Stackoverflow
Solution 3 - ReactjspapiroView Answer on Stackoverflow
Solution 4 - ReactjsDivyanshu MaithaniView Answer on Stackoverflow
Solution 5 - Reactjsosvaldo carrilloView Answer on Stackoverflow
Solution 6 - ReactjsYakovLView Answer on Stackoverflow
Solution 7 - ReactjsMehdi RaashView Answer on Stackoverflow
Solution 8 - ReactjsRamesh RView Answer on Stackoverflow
Solution 9 - Reactjsharun ugurView Answer on Stackoverflow
Solution 10 - ReactjsBar Horing AmirView Answer on Stackoverflow
Solution 11 - ReactjsbannedFromAskingQuestionsView Answer on Stackoverflow
Solution 12 - ReactjsManuel AbascalView Answer on Stackoverflow
Solution 13 - ReactjsVenkatesh ManoharView Answer on Stackoverflow
Solution 14 - ReactjsCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow