
Angular์ @Input
- ๋ถ๋ชจ ์ปดํฌ๋ํธ์์ ์์ ์ปดํฌ๋ํธ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ๋ ์ฌ์ฉ
- ์์ ์ปดํฌ๋ํธ์ ์์ ์ ์ ์ ํ๊ณ , ๋ถ๋ชจ ์ปดํฌ๋ํธ์์ ๊ฐ์ ์ ๋ฌํ ์ ์๋๋ก ํ๋ค.
- ๋ถ๋ชจ ์ปดํฌ๋ํธ์์ [childProperty]="parentValue"์ ๊ฐ์ด ์์ฑ์ ์ค์ ํ์ฌ ์์ ์ปดํฌ๋ํธ์ ๊ฐ์ ์ ๋ฌํ๋ค.
// parent.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-parent',
template: `
<div>
<app-child [childProperty]="parentValue"></app-child>
</div>
`,
})
export class ParentComponent {
parentValue = '๋ถ๋ชจ์์ ์ ๋ฌํ ๊ฐ';
}
// child.component.ts
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-child',
template: `
<div>
<p>์์ ์ปดํฌ๋ํธ ์์ฑ ๊ฐ: {{ childProperty }}</p>
</div>
`,
})
export class ChildComponent {
@Input() childProperty: string;
}
React์ props
- React์ props ๋ํ ๋ถ๋ชจ ์ปดํฌ๋ํธ์์ ์์ ์ปดํฌ๋ํธ๋ก ๊ฐ์ ์ ๋ฌํ ๋ ์ฌ์ฉํ๋ค.
- props๋ property์ ์ค๋ง๋ก, html ํ๊ทธ์ attribute์ ๊ตฌ๋ถ๋๋ค.
- React ์ปดํฌ๋ํธ๋ ํจ์๋ ํด๋์ค๋ก ๊ตฌํ๋ ์ ์์ผ๋ฉฐ, ํจ์ํ ์ปดํฌ๋ํธ๋ props๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์ ์ฌ์ฉํ๊ณ , ํด๋์คํ ์ปดํฌ๋ํธ๋ this.props๋ฅผ ํตํด ์ ๊ทผํ๋ค.
- ๋ถ๋ชจ ์ปดํฌ๋ํธ์์ childProperty={parentValue}์ ๊ฐ์ด ์์ฑ์ ์ง์ ํ์ฌ ์์ ์ปดํฌ๋ํธ์ ๊ฐ์ ์ ๋ฌํ๋ค.
// ํจ์ํ ์ปดํฌ๋ํธ
import React from 'react';
function ChildComponent(props) {
return (
<div>
<p>์์ ์ปดํฌ๋ํธ ์์ฑ ๊ฐ: {props.childProperty}</p>
</div>
);
}
// ํด๋์คํ ์ปดํฌ๋ํธ
import React, { Component } from 'react';
class ChildComponent extends Component {
render() {
return (
<div>
<p>์์ ์ปดํฌ๋ํธ ์์ฑ ๊ฐ: {this.props.childProperty}</p>
</div>
);
}
}
// ๋ถ๋ชจ ์ปดํฌ๋ํธ
import React from 'react';
import ChildComponent from './ChildComponent';
function ParentComponent() {
const parentValue = '๋ถ๋ชจ์์ ์ ๋ฌํ ๊ฐ';
return (
<div>
<ChildComponent childProperty={parentValue} />
</div>
);
}
๋ฐ์ํ
'Frontend > Angular vs React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Angular vs React | ์๋ช ์ฃผ๊ธฐ ๋ฉ์๋์ ๋ํด์ (0) | 2023.12.06 |
---|---|
Angular vs React | ์ ํ๋ฆฌ์ผ์ด์ ์ง์ ์ ์ ๋ํด์ (0) | 2023.11.16 |
Angular vs React | ๋์ ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ์ ๋ํด์ (0) | 2023.11.15 |
Angular vs React | ๊ฐ์ DOM์ ๋ํด์ (0) | 2023.10.11 |
Angular vs React | ๊ณตํต์ (0) | 2023.10.11 |