freeCodeCamp/curriculum/challenges/japanese/03-front-end-development-li.../react/pass-props-to-a-stateless-f...

5.3 KiB

id title challengeType forumTopicId dashedName
5a24c314108439a4d4036169 ステートレス関数型コンポーネントに props を渡す 6 301402 pass-props-to-a-stateless-functional-component

--description--

ここまでのチャレンジでは、React での JSX 要素、関数型コンポーネント、ES6 スタイルクラスコンポーネントの作成と構築について、多くのことを説明しました。 これらを基礎的な知識として、もう一つ React でごく一般的な機能である props について見てみましょう。 React では props、つまりプロパティを子コンポーネントに渡すことができます。 たとえば App コンポーネントがあり、そこでステートレス関数型コンポーネントである Welcome という子コンポーネントをレンダーするとします。 次のように記述することで、Welcomeuser プロパティを渡すことができます。

<App>
  <Welcome user='Mark' />
</App>

作成したのはカスタムの HTML 属性であり、React によってサポートされ、コンポーネントに渡すことができます。 この例では、作成したプロパティ user がコンポーネント Welcome に渡されます。 Welcome はステートレス関数型コンポーネントなので、次のようにしてこの値にアクセスできます。

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

この値のことを props と呼ぶのが標準的であり、ステートレス関数型コンポーネントを扱う際は基本的に、JSX を返す関数への引数として考えます。 関数本体で引数の値にアクセスすることができます。 クラスコンポーネントの場合は、こうした操作は多少異なります。

--instructions--

コードエディターに Calendar コンポーネントと CurrentDate コンポーネントがあります。 Calendar コンポーネントから CurrentDate をレンダーするときに、JavaScript の Date オブジェクトからの現在の日付が割り当てられた date のプロパティを渡してください。 次に、CurrentDate コンポーネントでこの prop にアクセスして、p タグの中に値を表示してください。 prop の値を JavaScript として評価するには、たとえば date={Date()} のように中括弧で囲む必要があります。

--hints--

Calendar コンポーネントから単一の div 要素を返します。

assert(
  (function () {
    const mockedComponent = Enzyme.mount(React.createElement(Calendar));
    return mockedComponent.children().type() === 'div';
  })()
);

Calendar コンポーネントの 2 つ目の子を CurrentDate コンポーネントにします。

assert(
  (function () {
    const mockedComponent = Enzyme.mount(React.createElement(Calendar));
    return mockedComponent.children().childAt(1).name() === 'CurrentDate';
  })()
);

CurrentDate コンポーネントに date という prop を持たせます。

assert(
  (function () {
    const mockedComponent = Enzyme.mount(React.createElement(Calendar));
    return mockedComponent.children().childAt(1).props().date;
  })()
);

CurrentDatedate prop に、テキストの文字列を含めます。

assert(
  (function () {
    const mockedComponent = Enzyme.mount(React.createElement(Calendar));
    const prop = mockedComponent.children().childAt(1).props().date;
    return typeof prop === 'string' && prop.length > 0;
  })()
);

Date() を呼び出して date prop を生成します。

assert(/<CurrentDatedate={Date\(\)}\/>/.test(__helpers.removeWhiteSpace(code)));

CurrentDate コンポーネントで、date prop からの値を p タグにレンダーします。

let date = 'dummy date';
assert(
  (function () {
    const mockedComponent = Enzyme.mount(
      React.createElement(CurrentDate, { date })
    );
    return mockedComponent.find('p').html().includes(date);
  })()
);

--seed--

--after-user-code--

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

--seed-contents--

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>
    );
  }
};

--solutions--

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>
    );
  }
};