4.6 KiB
4.6 KiB
id | title | challengeType | isRequired | videoUrl | localeTitle |
---|---|---|---|---|---|
5a24c314108439a4d403616f | Review Using Props with Stateless Functional Components | 6 | false | 查看使用无状态功能组件的道具 |
Description
React.Component
的类,但不使用内部状态(在下一个挑战中涵盖)。最后,有状态组件是保持其自身内部状态的任何组件。您可能会看到有状态组件简称为组件或React组件。一种常见的模式是尽可能地减少有状态并创建无状态功能组件。这有助于将状态管理包含到应用程序的特定区域。反过来,通过更容易地了解状态更改如何影响其行为,这可以改善应用程序的开发和维护。 Instructions
CampSite
组件,它将Camper
组件呈现为子组件。定义Camper
组件并为其指定{ name: 'CamperBot' }
默认道具。在Camper
组件内部,渲染您想要的任何代码,但要确保有一个p
元素仅包含作为prop
传递的name
值。最后,在Camper
组件上定义propTypes
,要求将name
作为prop提供,并验证它是string
类型。 Tests
tests:
- text: <code>CampSite</code>组件应该呈现。
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(CampSite)); return mockedComponent.find("CampSite").length === 1; })(), "The <code>CampSite</code> component should render.");'
- text: <code>Camper</code>组件应呈现。
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(CampSite)); return mockedComponent.find("Camper").length === 1; })(), "The <code>Camper</code> component should render.");'
- text: <code>Camper</code>组件应该包含默认道具,它将字符串<code>CamperBot</code>分配给键<code>name</code> 。
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 <code>Camper</code> component should include default props which assign the string <code>CamperBot</code> to the key <code>name</code>.");'
- text: <code>Camper</code>组件应包含要求<code>name</code> prop为<code>string</code>类型的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 <code>Camper</code> component should include prop types which require the <code>name</code> prop to be of type <code>string</code>.");'
- text: <code>Camper</code>组件应包含一个<code>p</code>元素,其中只包含<code>name</code> prop的文本。
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(CampSite)); return mockedComponent.find("p").text() === mockedComponent.find("Camper").props().name; })(), "The <code>Camper</code> component should contain a <code>p</code> element with only the text from the <code>name</code> prop.");'
Challenge Seed
class CampSite extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<Camper/>
</div>
);
}
};
// change code below this line
Before Test
var PropTypes = {
string: { isRequired: true }
};
After Test
console.info('after the test');
Solution
// solution required