如何在React组件的渲染方法中使用注释?

我有以下组件:

'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;

我的评论显示在UI中。

在组件的呈现方法中应用单行和多行注释的正确方法是什么?


当前回答

根据官方网站,这是两种方式:

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

第二个例子:

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

参考资料如下:如何在JSX中编写注释?

其他回答

有条件的呈现

React文档中提到的这种方法也适用于嵌套的/**/注释,不像{/**/}方法,例如:

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

完整的例子:

<!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/standalone@7.14.7/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>

只渲染前后。

啊,刚刚注意到,这样做的一个缺点是,像typescript这样的linter可能会抱怨“注释”中不正确的东西。

这就是方法。

有效:

...
render() {

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

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

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

}
...

无效:

...
render() {

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

       {//
        Invalid comment
       //}
    </p>
  )

}
...
{
    // Any valid JavaScript expression
}

如果您想知道为什么它可以工作,那是因为大括号{}内的所有内容都是JavaScript表达式。

所以这也很好:

{ /*
         Yet another JavaScript expression
*/ }

除了其他答案,还可以在JSX开始或结束之前或之后使用单行注释。以下是一个完整的总结:

有效的

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

如果我们要在JSX呈现逻辑中使用注释:

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

在声明props时,可以使用单行注释:

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

无效的

当在JSX中使用单行或多行注释而不使用{}包装它们时,注释将被呈现给UI:

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

下面是另一种允许你使用//来包含注释的方法:

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

这里的问题是,使用这种方法不能包含一行注释。例如,这是行不通的:

{// your comment cannot be like this}

因为右括号}被认为是注释的一部分,因此被忽略,这会抛出一个错误。