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>
  );
}
λ°˜μ‘ν˜•