freeCodeCamp/curriculum/challenges/chinese/03-front-end-libraries/react/review-using-props-with-sta...

4.6 KiB
Raw Blame History

id title challengeType isRequired videoUrl localeTitle
5a24c314108439a4d403616f Review Using Props with Stateless Functional Components 6 false 查看使用无状态功能组件的道具

Description

除了最后的挑战,你已经将道具传递给无状态功能组件。这些组件就像纯函数一样。他们接受道具作为输入,并在每次传递相同的道具时返回相同的视图。您可能想知道状态是什么,下一个挑战将更详细地介绍它。在此之前,这里是对组件术语的回顾。 无状态功能组件是您编写的任何接受道具并返回JSX的函数。另一方面 无状态组件是扩展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