読み方のヒント

矢印ボタン(または ← → キー)でページを移動できます。

第3節 子要素と関数を渡す

3.1 children で中身を受け取る

同じ枠の中に、場所ごとに違う中身を入れる部品を作ります。

import type { ReactNode } from "react";

type PanelProps = {
  title: string;
  children: ReactNode;
};

export const Panel = ({ title, children }: PanelProps) => {
  return (
    <section className="panel">
      <h2>{title}</h2>
      {children}
    </section>
  );
};
<Panel title="お知らせ">
  <p>11月3日は、祝日のため 10:00 から営業します。</p>
</Panel>

<Panel title="予約について">
  <p>受け取りの前日 20:00 まで受け付けます。</p>
  <p>1回の予約は合計10個までです。</p>
</Panel>

開くタグと閉じるタグのあいだに書いた中身は、children という名前の props として渡されます。ReactNode は、JSX・文字・数・null など、画面に表示できるものすべてを表す React の型です。

props渡し方型
titletitle="お知らせ"string
childrenタグのあいだに書くReactNode

10 / 16