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