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?

More than 1 year has passed since last update.

Angularにはprops概念は無いのでpropsを学ぶ。

Posted at

chatGPTで勉強をしています。
まず、筆者は Angularでよく開発しています。

Angularには、propsはありますか?

Angularには、Reactのpropsと直接的に対応する概念として「入力(Inputs)」と「出力(Outputs)」があります。

Reactでは、propsを使用して親コンポーネントから子コンポーネントへデータを渡します。同様に、Angularでは@Input()デコレータを使用して親コンポーネントから子コンポーネントへデータを渡すことができます。

InputとOutputの実装例を元に使い方

もちろん、Angularの@Input()@Output()デコレータを使って、親子コンポーネント間のデータの受け渡しとイベントの伝播の方法を詳しく説明します。

1. @Input()を使用してデータを子コンポーネントに渡す

親コンポーネントであるParentComponentChildComponentにデータを渡す例を見てみましょう。

ParentComponent:

@Component({
  selector: 'app-parent',
  template: `
    <app-child [childMessage]="parentMessage"></app-child>
  `
})
export class ParentComponent {
  parentMessage = 'Hello from Parent Component!';
}

ChildComponent:

@Component({
  selector: 'app-child',
  template: `
    <div>{{ childMessage }}</div>
  `
})
export class ChildComponent {
  @Input() childMessage: string;
}

この例では、ParentComponentChildComponentchildMessageという名前の@Input()を通じてデータを渡しています。

2. @Output()を使用して子コンポーネントから親コンポーネントへイベントを伝播する

子コンポーネントでのボタンクリックイベントを親コンポーネントに伝える例を見てみましょう。

ChildComponent:

import { Component, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-child',
  template: `
    <button (click)="sendEvent()">Click me!</button>
  `
})
export class ChildComponent {
  @Output() childEvent = new EventEmitter<string>();

  sendEvent() {
    this.childEvent.emit('Event from Child Component');
  }
}

ParentComponent:

@Component({
  selector: 'app-parent',
  template: `
    <app-child (childEvent)="handleChildEvent($event)"></app-child>
  `
})
export class ParentComponent {
  handleChildEvent(event: string) {
    console.log(event);  // Will log: "Event from Child Component"
  }
}

この例では、ChildComponentchildEventという名前の@Output()イベントを発火させ、それをParentComponentが受け取っています。

このように、Angularの@Input()@Output()デコレータを使用することで、親子コンポーネント間のデータの受け渡しとイベントの伝播を簡単に実現することができます。

Reactのpropsとは

Reactのpropsは、Reactコンポーネントの入力として受け取るデータのことを指します。propsは、コンポーネントの再利用性を高めるために、親コンポーネントから子コンポーネントへデータを渡す際に使用されます。propsは読取専用であり、子コンポーネント内でその値を変更することはできません。

以下は、Reactのpropsの基本的な使用方法を示す例です。

親コンポーネント:

function ParentComponent() {
  return <ChildComponent message="Hello from Parent!" />;
}

子コンポーネント:

function ChildComponent(props) {
  return <div>{props.message}</div>;
}

この例では、ParentComponentからChildComponentmessageという名前のpropsが渡されています。子コンポーネント内では、このpropsを使用してメッセージを表示しています。

propsの特徴:

  1. 読取専用: propsは不変であり、コンポーネント内でその値を変更することはできません。
  2. 親から子へ: propsは常に親コンポーネントから子コンポーネントへデータを渡すために使用されます。
  3. 任意のデータを渡せる: 文字列、数値、配列、オブジェクト、関数、React要素など、ほとんどのタイプのデータをpropsとして渡すことができます。
  4. 型チェック: propTypesを使用することで、propsの型を指定し、開発中に型の不一致に関する警告を受け取ることができます。

総じて、propsはReactコンポーネント間のデータの受け渡しを容易にし、コンポーネントの再利用性と予測可能性を高めるための重要な概念です。

propsの実装例

もちろん、Reactのpropsに関する詳しい使い方と実装のポイントを、上記の実装例をベースに説明いたします。

1. propsの基本的な使用方法

親コンポーネントから子コンポーネントにデータを渡す場合、親コンポーネントのJSX内で属性としてデータを設定します。

親コンポーネント:

function ParentComponent() {
  return <ChildComponent message="Hello from Parent!" />;
}

子コンポーネントでは、このデータをpropsオブジェクトを通じて受け取ります。

子コンポーネント:

function ChildComponent(props) {
  return <div>{props.message}</div>;
}

2. 複数のpropsを渡す

1つ以上のpropsを子コンポーネントに渡すことができます。

function ParentComponent() {
  return <ChildComponent message="Hello" author="Parent" />;
}

function ChildComponent(props) {
  return <div>{props.message} from {props.author}!</div>;
}

3. 子コンポーネント内でpropsを変更しない

propsは読取専用です。子コンポーネント内で直接変更することはできません。変更を試みると、Reactはエラーを投げます。

4. デフォルトのpropsを設定する

defaultPropsを使用して、propsにデフォルトの値を設定することができます。

function ChildComponent(props) {
  return <div>{props.message}</div>;
}

ChildComponent.defaultProps = {
  message: "Default Message"
};

5. propsの型チェック

propTypesを使用して、propsの型を指定し、型の不一致に関する警告を受け取ることができます。prop-typesライブラリを使用する必要があります。

import PropTypes from 'prop-types';

function ChildComponent(props) {
  return <div>{props.message}</div>;
}

ChildComponent.propTypes = {
  message: PropTypes.string.isRequired
};

この例では、messageが文字列であることを期待しており、必須のpropsであることを示しています。

6. 子から親へのデータの伝達

子コンポーネントは、関数をpropsとして受け取り、その関数を実行することで親コンポーネントにデータやイベントを伝えることができます。

function ParentComponent() {
  function handleChildClick(data) {
    console.log(data);
  }
  
  return <ChildComponent onClick={handleChildClick} />;
}

function ChildComponent(props) {
  return <button onClick={() => props.onClick("Data from Child")}>Click me</button>;
}

この方法で、子コンポーネントから親コンポーネントへの通信を実現できます。

これらの基本的なポイントを理解することで、Reactのpropsを効果的に使用して、コンポーネント間のデータの受け渡しを行うことができます。

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?