Files
freeCodeCamp/curriculum/challenges/chinese/03-front-end-libraries/react/add-comments-in-jsx.md
Oliver Eyton-Williams dec409a4bd fix: s/localeTitle/title/g
2020-10-06 23:10:08 +05:30

2.0 KiB
Raw Blame History

id, challengeType, forumTopicId, title
id challengeType forumTopicId title
5a24bbe0dba28a8d3cbd4c5e 6 301376 在 JSX 中添加注释

Description

JSX 是一种可以编译成有效 JavaScript 的语法。有时为了便于阅读你可能需要在代码中添加注释。像大多数编程语言一样JSX 也有自己的方法来实现这一点。 要将注释放在 JSX 中,可以使用{/* */}语法来包裹注释文本。

Instructions

代码编辑器中的 JSX 元素与你在上一个挑战中创建的元素类似。在提供的div元素中的某处添加注释,而不修改现有的h1p元素。

Tests

tests:
  - text: 常量<code>JSX</code>应该返回一个<code>div</code>元素。
    testString: assert(JSX.type === 'div');
  - text: <code>div</code>应该包含一个<code>h1</code>标签作为第一个元素。
    testString: assert(JSX.props.children[0].type === 'h1');
  - text: <code>div</code>应该包含一个<code>p</code>标签作为第二个元素。
    testString: assert(JSX.props.children[1].type === 'p');
  - text: 当前的 <code>h1</code> 和 <code>p</code> 元素不能被修改。
    testString: assert(JSX.props.children[0].props.children === 'This is a block of JSX' && JSX.props.children[1].props.children === 'Here\'s a subtitle');     
  - text: <code>JSX</code>应该包含一个注释。
    testString: assert(/<div>[\s\S]*{\s*\/\*[\s\S]*\*\/\s*}[\s\S]*<\/div>/.test(code));

Challenge Seed

const JSX = (
  <div>
    <h1>This is a block of JSX</h1>
    <p>Here's a subtitle</p>
  </div>
);

After Test

ReactDOM.render(JSX, document.getElementById('root'))

Solution

const JSX = (
<div>
  <h1>This is a block of JSX</h1>
  { /* this is a JSX comment */ }
  <p>Here's a subtitle</p>
</div>);