1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【React】要素を指定してPropsの型を取得する方法【TypeScript】

Last updated at Posted at 2024-04-03

ComponentProps

ComponentPropsを使用すると指定したコンポーネントが受け取るpropsの型を取得できます。

refを除外した場合にはReact.ComponentPropsWithoutRefを使用します。


type Props = React.ComponentPropsWithoutRef<'button'>

const MyComponent = (props: Props) => {
  return <button {...props} />
}

refを明示的に含めたい場合にはReact.ComponentPropsWithRefを使用します。


type Props = React.ComponentPropsWithRef<'button'>

const MyComponent = (props: Props) => {
  return <button {...props} />
}

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?