如何在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中。

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


当前回答

这就是方法。

有效:

...
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>
  )

}
...

其他回答

下面是React中注释的6种方式:

多行TypeScript注释 属性注释 单行JSX注释 单行JSX注释 多行JSX注释 单行JavaScript注释

/**
 * 1. Multi-line
 * TypeScript comment
 * @constructor
 */

export const GoodQuote = observer(({model} : { model: HomeModel }) => {

    console.log(model.selectedIndex)
    return useObserver(() =>
        <div /* 2. HTML attribute comment */ onClick={() => model.toggleQuote()}>
            <p>{model.quotes[model.selectedIndex]}</p>
            {
                // 3. Single-line comment
            }
            { /* 4. True Single-line comment */}
            { /*
              5. Multi-line
              React comment
            */ }
        </div> // 6. Javascript style comment

    )
})

这就是方法。

有效:

...
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>
  )

}
...

除了其他答案,还可以在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>
)

另一方面,下面是一个有效的注释,直接从一个工作的应用程序:

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

显然,在JSX元素的尖括号内,//语法是有效的,但{/**/}是无效的。以下是休息时间:

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

在呈现方法中允许使用注释,但是为了在JSX中使用它们,必须将它们括在大括号中并使用多行风格的注释。

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

您可以在这里阅读更多关于JSX中注释如何工作的信息。