5
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・TypeScript】実は、Props引数はスプレッド構文を渡すことができる

Last updated at Posted at 2023-03-15

はじめに

以下本を読んだときに、Propsについて学びがあったので、TypeScriptで記事にしました。

https://www.oreilly.co.jp/books/9784873119380/

Props引数をスプレッド構文で受け取る

例えば、以下コンポーネントがあるとする

type Props = { message: string, no: number };

const ChildrenText = ({ message, no }: Props) => {
  return (
    <>
      <div>Message:{message}</div>
      <div>No:{no}</div>
    </>
  );
};

親コンポーネントがChildrenText にProps引数を設定する場合、以下のようにスプレッド構文で渡すことができる

const Parent = () => {
  const props = {
    message: 'あああああ',
    no: 114514
  };

  return (
    <div className="App" >
		{/* スプレッド構文でPropsを渡す */}      
		<ChildrenText {...props} />
    </div >
  );
}
5
1
1

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