0
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?

Propsの型定義について学ぶ

Posted at

コンポーネントがどんなPropsを受け取っているのかが事前にわかると、コードのタイプミスなどに気づくことができたり、型定義をすることで他の人がコードを読むときスムーズになります。

子コンポーネント側の型定義の仕方の一例です。

type User = {
  id: number;
  name: string;
};

export const UserList = (props: User) => {
  const { id, name } = props;

  return (
    <p>{id}: {name}</p>
  );
};

上記のように型定義をすることで、型定義した値が抜けていたり、propsで渡す名前が間違えているといった場合はコンパイル・ビルド時にエラーを返せるようになります。

大規模なシステムほどTypeScriptを導入した方が良いと言われるのは、やはり変更(リファクタリング)に強いためです。

0
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
0
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?