Files
freeCodeCamp/curriculum/challenges/chinese/03-front-end-libraries/react/pass-props-to-a-stateless-functional-component.md
Oliver Eyton-Williams dec409a4bd fix: s/localeTitle/title/g
2020-10-06 23:10:08 +05:30

4.7 KiB
Raw Blame History

id, challengeType, forumTopicId, title
id challengeType forumTopicId title
5a24c314108439a4d4036169 6 301402 将 Props 传递给无状态函数组件

Description

之前的挑战涵盖了关于在 React 中创建和组合 JSX 元素、函数组件和 ES6 风格的类组件的很多内容。有了这个基础,现在是时候看看 React 中的另一个常见特性 props 了。在 React 中,你可以将属性传递给子组件。假设你有一个App组件,该组件渲染了一个名为Welcome的子组件,它是一个无状态函数组件。你可以通过以下方式给Welcome传递一个user属性:
<App>
  <Welcome user='Mark' />
</App>

使用自定义 HTML 属性React 支持将属性user传递给组件Welcome。由于Welcome是一个无状态函数组件,它可以像这样访问该值:

const Welcome = (props) => <h1>Hello, {props.user}!</h1>

调用props这个值是常见做法,当处理无状态函数组件时,你基本上可以将其视为返回 JSX 的函数的参数。这样,你就可以在函数体中访问该值。但对于类组件,访问方式会略有不同。

Instructions

代码编辑器中有CalendarCurrentDate组件。从Calendar组件渲染CurrentDate时,从 JavaScript 的Date对象分配当前日期,并将其作为date属性传入。然后访问CurrentDate组件的prop,并在p标签中显示其值。请注意,要将prop的值视为 JavaScript必须将它们括在花括号中例如date={Date()}

Tests

tests:
  - text: <code>Calendar</code>组件应该返回单个<code>div</code>元素。
    testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(Calendar)); return mockedComponent.children().type() === 'div'; })());
  - text: <code>Calendar</code>组件的第二个子元素应该是<code>CurrentDate</code>组件。
    testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(Calendar)); return mockedComponent.children().childAt(1).name() === 'CurrentDate'; })());
  - text: <code>CurrentDate</code>组件应该有一个名为<code>date</code>的属性。
    testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(Calendar)); return mockedComponent.children().childAt(1).props().date })());
  - text: <code>CurrentDate</code>的<code>date</code>属性应该包含一段文本字符串。
    testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(Calendar)); const prop = mockedComponent.children().childAt(1).props().date; return( typeof prop === 'string' && prop.length > 0 ); })());
  - text: <code>CurrentDate</code>组件应该把<code>date</code>属性渲染在<code>p</code>标签内。
    testString: assert(/<CurrentDatedate={Date\(\)}\/>/.test(code.replace(/\s/g, '')))
  - text: <code>CurrentDate</code> 组件应该把 <code>date</code> 属性渲染在 <code>p</code> 标签内。
    testString: let date = "dummy date"; assert((function() { const mockedComponent = Enzyme.mount(React.createElement(CurrentDate, {date})); return mockedComponent.find('p').html().includes(date); })());


Challenge Seed


const CurrentDate = (props) => {
  return (
    <div>
      { /* change code below this line */ }
      <p>The current date is: </p>
      { /* change code above this line */ }
    </div>
  );
};

class Calendar extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <div>
        <h3>What date is it?</h3>
        { /* change code below this line */ }
        <CurrentDate />
        { /* change code above this line */ }
      </div>
    );
  }
};

After Test

ReactDOM.render(<Calendar />, document.getElementById('root'))

Solution

const CurrentDate = (props) => {
  return (
    <div>
      { /* change code below this line */ }
      <p>The current date is: {props.date}</p>
      { /* change code above this line */ }
    </div>
  );
};

class Calendar extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <div>
        <h3>What date is it?</h3>
        { /* change code below this line */ }
        <CurrentDate date={Date()} />
        { /* change code above this line */ }
      </div>
    );
  }
};