2
1

More than 3 years have passed since last update.

TypeScriptを使ってReactのFunctionalComponentに型安全にpropsを渡す

Posted at
import React, { FunctionComponent } from 'react';

type Props = { title: string }
const Title: FunctionComponent<Props> = props => {
  return <>
    <h1>{props.title}</h1>
  </>
}

または、以下のようにtypeの定義省略することも出来る。

import React, { FunctionComponent } from 'react';

const Title: FunctionComponent<{ title: string }> = props => {
  return <>
    <h1>{props.title}</h1>
  </>
}

使用する時はこのように使う。

function App() {
  return <>
    <Title title="Hello World" />
  </>
}

関連記事

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