2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

TypeScript 学習中にFCを使って関数に型定義をしたが、childrenを渡した時にエラーとなった。

原因

React.FC は children を暗黙的に持つ挙動だと思っていたがReact 18以降は children をもたなくなったみたい。

    <Text color="pink" fontSize="18px">
        暗黙的に渡さないよ
    </Text>
import type { FC } from "react";

type Props = {
  color: string;
  fontSize: string;
};

export const Text: FC<Props> = (props) => {
  const { color, fontSize } = props;
  return <p style={{ color, fontSize }}>テキストです</p>;
};

解決

children は明示するして解決。

import type { FC, ReactNode } from "react";

type Props = {
  color: string;
  fontSize: string;
  children?: ReactNode;
};

export const Text: FC<Props> = (props) => {
  const { color, fontSize, children } = props;
  return <p style={{ color, fontSize }}>{children}</p>;
};

参考

ありがとうございます。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?