読み方のヒント

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

第2節 共通の部品を作る

2.2 props を増やしすぎない

共通の部品は、多くの場所で使うため、props を足したくなる場面がよくあります。

よくない例何が困るかよい例
color="#3d8049" のように色を受け取るトークンから外れた色が増えるvariant で決まった種類だけを選ぶ
isReserveButton のように、機能の名前の props共通の部品が、予約の機能を知る予約の側で、Button を使った部品を作る
marginTop を受け取る部品の外の余白を、部品が決めることになる並べる側の CSS で余白を決める

部品の外の余白(ほかの部品との間隔)は、並べる側が決めます。部品の中の余白は、部品が決めます。こうしておくと、同じボタンを、どの画面にも同じ形で置けます。

予約の機能に特有のボタンは、予約の機能の中で、共通の部品を使って作ります。

// src/features/reservations/components/SubmitReservationButton.tsx
import { Button } from "@/shared/ui/Button";

export const SubmitReservationButton = ({ isPending }: { isPending: boolean }) => (
  <Button type="submit" disabled={isPending}>
    {isPending ? "送っています…" : "予約する"}
  </Button>
);
解答の例

type TextFieldProps = ComponentPropsWithoutRef<"input"> & { label: string; error?: string | undefined; }; と書きます。中では、id を使って &lt;label htmlFor&gt; と結び付け、error があるときに aria-invalid と aria-describedby を付けます。

8 / 17