--- id: 5a24c314108439a4d4036165 title: Use React to Render Nested Components challengeType: 6 isRequired: false videoUrl: '' localeTitle: 使用React渲染嵌套组件 --- ## Description
最后一个挑战显示了组合两个组件的简单方法,但是有许多不同的方法可以使用React组合组件。组件组合是React强大的功能之一。当您使用React时,重要的是开始根据组件(如上一个挑战中的App示例)考虑您的用户界面。您将UI分解为其基本构建块,这些块成为组件。这有助于将负责UI的代码与负责处理应用程序逻辑的代码分开。它可以大大简化复杂项目的开发和维护。
## Instructions
代码编辑器中定义了两个功能组件,名为TypesOfFruitFruits 。取TypesOfFruit组分和构成它,或者它 ,所述内Fruits组分。然后使用Fruits组件并将其嵌套在TypesOfFood组件中。结果应该是一个嵌套在父组件中的子组件,它嵌套在它自己的父组件中!
## Tests
```yml tests: - text: TypesOfFood组件应返回单个div元素。 testString: assert(Enzyme.shallow(React.createElement(TypesOfFood)).type() === 'div'); - text: TypesOfFood组件应返回Fruits组件。 testString: assert(Enzyme.shallow(React.createElement(TypesOfFood)).props().children[1].type.name === 'Fruits'); - text: Fruits组件应返回TypesOfFruit组件。 testString: assert(Enzyme.mount(React.createElement(TypesOfFood)).find('h2').html() === '

Fruits:

'); - text: TypesOfFruit组件应返回h2ul元素。 testString: assert(Enzyme.mount(React.createElement(TypesOfFood)).find('ul').text() === 'ApplesBlueberriesStrawberriesBananas'); ```
## Challenge Seed
```jsx const TypesOfFruit = () => { return (

Fruits:

  • Apples
  • Blueberries
  • Strawberries
  • Bananas
); }; const Fruits = () => { return (
{ /* change code below this line */ } { /* change code above this line */ }
); }; class TypesOfFood extends React.Component { constructor(props) { super(props); } render() { return (

Types of Food:

{ /* change code below this line */ } { /* change code above this line */ }
); } }; ```
### After Test
```js console.info('after the test'); ```
## Solution
```js // solution required ```