Frontend/Angular vs React

Angular vs React | ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋กœ์˜ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ์— ๋Œ€ํ•ด์„œ

์ด์ œํฌ 2023. 11. 14. 18:00


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>
  );
}
๋ฐ˜์‘ํ˜•