props
或属性。这个挑战着眼于如何将数组作为props
传递。要将数组传递给JSX元素,必须将其视为JavaScript并用大括号括起来。 <为父级>然后子组件可以访问数组属性
<ChildComponent colors = {[“green”,“blue”,“red”]} />
</为父级>
colors
。访问属性时可以使用诸如join()
类的数组方法。 const ChildComponent = (props) => <p>{props.colors.join(', ')}</p>
这会将所有colors
数组项连接成逗号分隔的字符串并生成: <p>green, blue, red</p>
稍后,我们将了解在React中呈现数据数组的其他常用方法。 List
和ToDo
组件。从ToDo
组件渲染每个List
,传入分配给待办任务数组的tasks
属性,例如["walk dog", "workout"]
。然后在List
组件中访问此tasks
数组,在p
元素中显示其值。使用join(", ")
以逗号分隔列表的形式显示p
元素中的props.tasks
数组。今天的列表应该至少有2个任务,明天应该至少有3个任务。 ToDo
组件应返回单个外部div
。
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(ToDo)); return mockedComponent.children().first().type() === "div"; })(), "The ToDo
component should return a single outer div
.");'
- text: ToDo
组件的第三个子ToDo
应该是List
组件的实例。
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(ToDo)); return mockedComponent.children().first().childAt(2).name() === "List"; })(), "The third child of the ToDo
component should be an instance of the List
component.");'
- text: ToDo
组件的第五个子ToDo
应该是List
组件的一个实例。
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(ToDo)); return mockedComponent.children().first().childAt(4).name() === "List"; })(), "The fifth child of the ToDo
component should be an instance of the List
component.");'
- text: List
组件的两个实例都应该有一个名为tasks
的属性,而tasks
应该是array类型。
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(ToDo)); return Array.isArray(mockedComponent.find("List").get(0).props.tasks) && Array.isArray(mockedComponent.find("List").get(1).props.tasks); })(), "Both instances of the List
component should have a property called tasks
and tasks
should be of type array.");'
- text: 表示今天任务的第一个List
组件应该有2个或更多项。
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(ToDo)); return mockedComponent.find("List").get(0).props.tasks.length >= 2; })(), "The first List
component representing the tasks for today should have 2 or more items.");'
- text: 表示明天任务的第二个List
组件应该有3个或更多项。
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(ToDo)); return mockedComponent.find("List").get(1).props.tasks.length >= 3; })(), "The second List
component representing the tasks for tomorrow should have 3 or more items.");'
- text: 'List
组件应该将p
标记中的tasks
prop的值呈现为以逗号分隔的列表,例如walk dog, workout
。'
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(ToDo)); return mockedComponent.find("p").get(0).props.children === mockedComponent.find("List").get(0).props.tasks.join(", ") && mockedComponent.find("p").get(1).props.children === mockedComponent.find("List").get(1).props.tasks.join(", "); })(), "The List
component should render the value from the tasks
prop in the p
tag as a comma separated list, for example walk dog, workout
.");'
```
{}
{ /* change code above this line */ } }; class ToDo extends React.Component { constructor(props) { super(props); } render() { return (