--- id: 5a24c314108439a4d403616f title: Review Using Props with Stateless Functional Components challengeType: 6 isRequired: false videoUrl: '' localeTitle: 查看使用无状态功能组件的道具 --- ## Description
除了最后的挑战,你已经将道具传递给无状态功能组件。这些组件就像纯函数一样。他们接受道具作为输入,并在每次传递相同的道具时返回相同的视图。您可能想知道状态是什么,下一个挑战将更详细地介绍它。在此之前,这里是对组件术语的回顾。 无状态功能组件是您编写的任何接受道具并返回JSX的函数。另一方面, 无状态组件是扩展React.Component的类,但不使用内部状态(在下一个挑战中涵盖)。最后,有状态组件是保持其自身内部状态的任何组件。您可能会看到有状态组件简称为组件或React组件。一种常见的模式是尽可能地减少有状态并创建无状态功能组件。这有助于将状态管理包含到应用程序的特定区域。反过来,通过更容易地了解状态更改如何影响其行为,这可以改善应用程序的开发和维护。
## Instructions
代码编辑器有一个CampSite组件,它将Camper组件呈现为子组件。定义Camper组件并为其指定{ name: 'CamperBot' }默认道具。在Camper组件内部,渲染您想要的任何代码,但要确保有一个p元素仅包含作为prop传递的name值。最后,在Camper组件上定义propTypes ,要求将name作为prop提供,并验证它是string类型。
## Tests
```yml tests: - text: CampSite组件应该呈现。 testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(CampSite)); return mockedComponent.find("CampSite").length === 1; })(), "The CampSite component should render.");' - text: Camper组件应呈现。 testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(CampSite)); return mockedComponent.find("Camper").length === 1; })(), "The Camper component should render.");' - text: Camper组件应该包含默认道具,它将字符串CamperBot分配给键name 。 testString: 'getUserInput => assert((function() { const noWhiteSpace = getUserInput("index").replace(/\s/g, ""); const verify1 = "Camper.defaultProps={name:\"CamperBot\"}"; const verify2 = "Camper.defaultProps={name:"CamperBot"}"; return (noWhiteSpace.includes(verify1) || noWhiteSpace.includes(verify2)); })(), "The Camper component should include default props which assign the string CamperBot to the key name.");' - text: Camper组件应包含要求name prop为string类型的prop类型。 testString: 'getUserInput => assert((function() { const mockedComponent = Enzyme.mount(React.createElement(CampSite)); const noWhiteSpace = getUserInput("index").replace(/\s/g, ""); const verifyDefaultProps = "Camper.propTypes={name:PropTypes.string.isRequired}"; return noWhiteSpace.includes(verifyDefaultProps); })(), "The Camper component should include prop types which require the name prop to be of type string.");' - text: Camper组件应包含一个p元素,其中只包含name prop的文本。 testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(CampSite)); return mockedComponent.find("p").text() === mockedComponent.find("Camper").props().name; })(), "The Camper component should contain a p element with only the text from the name prop.");' ```
## Challenge Seed
```jsx class CampSite extends React.Component { constructor(props) { super(props); } render() { return (
); } }; // change code below this line ```
### Before Test
```jsx var PropTypes = { string: { isRequired: true } }; ```
### After Test
```js console.info('after the test'); ```
## Solution
```js // solution required ```