React.Component
的类,但不使用内部状态(在下一个挑战中涵盖)。最后,有状态组件是保持其自身内部状态的任何组件。您可能会看到有状态组件简称为组件或React组件。一种常见的模式是尽可能地减少有状态并创建无状态功能组件。这有助于将状态管理包含到应用程序的特定区域。反过来,通过更容易地了解状态更改如何影响其行为,这可以改善应用程序的开发和维护。 CampSite
组件,它将Camper
组件呈现为子组件。定义Camper
组件并为其指定{ name: 'CamperBot' }
默认道具。在Camper
组件内部,渲染您想要的任何代码,但要确保有一个p
元素仅包含作为prop
传递的name
值。最后,在Camper
组件上定义propTypes
,要求将name
作为prop提供,并验证它是string
类型。 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.");'
```