freeCodeCamp/curriculum/challenges/chinese/03-front-end-libraries/react/give-sibling-elements-a-uni...

3.5 KiB
Raw Blame History

id title challengeType isRequired videoUrl localeTitle
5a24c314108439a4d403618b Give Sibling Elements a Unique Key Attribute 6 false 为兄弟元素提供唯一的键属性

Description

最后一项挑战展示了如何使用map方法根据用户输入动态呈现多个元素。但是,这个例子中缺少一个重要的部分。创建元素数组时,每个元素都需要将key属性设置为唯一值。 React使用这些键来跟踪添加更改或删除的项目。当以任何方式修改列表时这有助于使重新呈现过程更有效。请注意键只需要在兄弟元素之间是唯一的它们在您的应用程序中不需要是全局唯一的。

Instructions

代码编辑器有一个包含一些前端框架的数组和一个名为Frameworks()的无状态功能组件。 Frameworks()需要将数组映射到无序列表,就像上一次挑战一样。完成编写map回调以返回frontEndFrameworks数组中每个框架的li元素。这一次,请确保为每个li一个key属性,设置为唯一值。通常,您希望使键成为唯一标识要呈现的元素的键。作为最后的手段,可以使用数组索引,但通常您应该尝试使用唯一标识。

Tests

tests:
  - text: <code>Frameworks</code>组件应该存在并呈现给页面。
    testString: 'assert(Enzyme.mount(React.createElement(Frameworks)).find("Frameworks").length === 1, "The <code>Frameworks</code> component should exist and render to the page.");'
  - text: <code>Frameworks</code>应该呈现<code>h1</code>元素。
    testString: 'assert(Enzyme.mount(React.createElement(Frameworks)).find("h1").length === 1, "<code>Frameworks</code> should render an <code>h1</code> element.");'
  - text: <code>Frameworks</code>应该呈现<code>ul</code>元素。
    testString: 'assert(Enzyme.mount(React.createElement(Frameworks)).find("ul").length === 1, "<code>Frameworks</code> should render a <code>ul</code> element.");'
  - text: ''
    testString: 'assert(Enzyme.mount(React.createElement(Frameworks)).find("ul").children().length === 6 && Enzyme.mount(React.createElement(Frameworks)).find("ul").childAt(0).name() === "li" && Enzyme.mount(React.createElement(Frameworks)).find("li").length === 6, "The <code>ul</code> tag should render 6 child <code>li</code> elements.");'
  - text: 每个列表项元素都应具有唯一的<code>key</code>属性。
    testString: 'assert((() => { const ul = Enzyme.mount(React.createElement(Frameworks)).find("ul"); const keys = new Set([ ul.childAt(0).key(), ul.childAt(1).key(), ul.childAt(2).key(), ul.childAt(3).key(), ul.childAt(4).key(), ul.childAt(5).key(), ]); return keys.size === 6; })(), "Each list item element should have a unique <code>key</code> attribute.");'

Challenge Seed

const frontEndFrameworks = [
  'React',
  'Angular',
  'Ember',
  'Knockout',
  'Backbone',
  'Vue'
];

function Frameworks() {
  const renderFrameworks = null; // change code here
  return (
    <div>
      <h1>Popular Front End JavaScript Frameworks</h1>
      <ul>
        {renderFrameworks}
      </ul>
    </div>
  );
};

After Test

console.info('after the test');

Solution

// solution required