5 minute read

๋ณธ ๊ฒŒ์‹œ๊ธ€์€ย ๋ฆฌ์•กํŠธ ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ฝ๊ณ  ์ •๋ฆฌํ•œ ๊ธ€์ด๋‹ค.

: ์—˜๋ฆฌ๋จผํŠธ ๋ Œ๋”๋ง ํŒŒํŠธ์—์„œ ๋‹ค๋ค„๋ณธ ์‹œ๊ณ„ ์˜ˆ์‹œ๋ฅผ ๋‹ค์‹œ ์‚ดํŽด๋ณด์ž. ๋ Œ๋”๋ง ๋œ ์ถœ๋ ฅ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด root.render()๋ฅผ ํ˜ธ์ถœํ–ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
const root = ReactDOM.createRoot(document.getElementById("root"));

function tick() {
  const element = (
    <div>
      <h1>Hello, world!</h1>
      <h2>It is {new Date().toLocaleTimeString()}.</h2>
    </div>
  );
  root.render(element);
}

setInterval(tick, 1000);

: ์ด ์„น์…˜์—์„œ Clock์ปดํฌ๋„ŒํŠธ๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๊ณ  ์บก์Šํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์šธ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์ปดํฌ๋„ŒํŠธ๋Š” ์Šค์Šค๋กœ ํƒ€์ด๋จธ๋ฅผ ์„ค์ •ํ•˜๊ณ  ๋งค ์ดˆ ์Šค์Šค๋กœ ์—…๋ฐ์ดํŠธํ•  ๊ฒƒ์ด๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const root = ReactDOM.createRoot(document.getElementById("root"));

function Clock(props) {
  return (
    <div>
      <h1>Hello, world!</h1>
      <h2>It is {props.date.toLocaleTimeString()}.</h2>
    </div>
  );
}

function tick() {
  root.render(<Clock date={new Date()} />);
}

setInterval(tick, 1000);
  • ์‹œ๊ณ„๋ฅผ ์บก์Šํ™” ํ•˜๊ธฐ์ „ ์—ฌ๊ธฐ์—๋Š” ์ค‘์š”ํ•œ ์กฐ๊ฑด์ด ๋ˆ„๋ฝ๋˜์–ด ์žˆ๋Š”๋ฐ, Clock์ด ํƒ€์ด๋จธ๋ฅผ ์„ค์ •ํ•˜๊ณ  ๋งค์ดˆ UI๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ฒƒ์€ย Clock์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์— ์žˆ์–ด์•ผ ํ•œ๋‹ค.

: ์ด์ƒ์ ์œผ๋กœ ํ•œ ๋ฒˆ๋งŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  Clock์€ ์Šค์Šค๋กœ ์—…๋ฐ์ดํŠธํ•˜๋„๋ก ๋งŒ๋“ค์–ด๋ณด์ž.

1
root.render(<Clock />);
  • ์ด๊ฒƒ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ Clock์ปดํฌ๋„ŒํŠธ์— โ€œstateโ€๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค.
  • State๋Š” Props์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ, ๋น„๊ณต๊ฐœ์ด๋ฉฐ ์ปดํฌ๋„ŒํŠธ์— ์˜ํ•ด ์™„์ „ํžˆ ์ œ์–ด๋œ๋‹ค.

ํ•จ์ˆ˜๋ฅผ ํด๋ž˜์Šค๋กœ ๋ณ€ํ™˜

: ๋‹ค์„ฏ ๋‹จ๊ณ„๋กœ Clockย ๊ณผ ๊ฐ™์€ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ๋ฅผ ํด๋ž˜์Šค๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. React.Componentย ๋ฅผ ํ™•์žฅํ•˜๋Š” ๋™์ผํ•œ ์ด๋ฆ„์˜ ES6 class๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
  2. ๋น„์–ด์žˆ๋Š”ย render()ย ๋ฉ”์„œ๋“œ๋ฅผ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•œ๋‹ค.
  3. ํ•จ์ˆ˜์˜ ๋‚ด์šฉ์„ย render()ย ๋ฉ”์„œ๋“œ ์•ˆ์œผ๋กœ ์˜ฎ๊ธด๋‹ค.
  4. render()ย ๋ฐ”๋”” ๋‚ด์—์„œย propsย ๋ฅผย this.propsย ๋กœ ๋ฐ”๊พผ๋‹ค.
  5. ๋‚จ์•„์žˆ๋Š” ๋นˆ ํ•จ์ˆ˜ ์„ ์–ธ๋ฌธ์„ ์ œ๊ฑฐํ•œ๋‹ค.
1
2
3
4
5
6
7
8
9
10
class Clock extends React.Component {
  render() {
    return (
      <div>
        <h1>Hello, world!</h1>
        <h2>It is {this.props.date.toLocaleTimeString()}.</h2>
      </div>
    );
  }
}
  • Clockย ์€ ์ด์ œ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ํด๋ž˜์Šค๋กœ ์ •์˜๋œ๋‹ค.

Class์— ๋กœ์ปฌ state ์ถ”๊ฐ€ํ•˜๊ธฐ

: date๋ฅผ props์—์„œ state๋กœ ์˜ฎ๊ธฐ๊ธฐ ์œ„ํ•ด์„œ 3๋‹จ๊ณ„๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.

1๏ธโƒฃ render()ย ๋ฉ”์„œ๋“œ ๋‚ด์˜ย this.props.date๋ฅผย this.state.date๋กœ ๋ฐ”๊พผ๋‹ค.

1
2
3
4
5
6
7
8
9
10
class Clock extends React.Component {
  render() {
    return (
      <div>
        <h1>Hello, world!</h1>
        <h2>It is {this.state.date.toLocaleTimeString()}.</h2>
      </div>
    );
  }
}

2๏ธโƒฃ this.state๋ฅผ ์ดˆ๊ธฐํ™” ํ•˜๋Š”ย ํด๋ž˜์Šค ์ƒ์„ฑ์žย ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = { date: new Date() };
  }

  render() {
    return (
      <div>
        <h1>Hello, world!</h1>
        <h2>It is {this.state.date.toLocaleTimeString()}.</h2>
      </div>
    );
  }
}

ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ๋Š” ํ•ญ์ƒ props๋กœ ๊ธฐ๋ณธ constructor๋ฅผ ํ˜ธ์ถœํ•ด์•ผํ•œ๋‹ค.

3๏ธโƒฃ <Clock />์š”์†Œ์—์„œย dateย prop์„ ์‚ญ์ œํ•œ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = { date: new Date() };
  }

  render() {
    return (
      <div>
        <h1>Hello, world!</h1>
        <h2>It is {this.state.date.toLocaleTimeString()}.</h2>
      </div>
    );
  }
}

ReactDOM.render(<Clock />, document.getElementById("root"));

ํด๋ž˜์Šค์— ๋ผ์ดํŠธ์‚ฌ์ดํด ๋ฉ”์„œ๋“œ ์ถ”๊ฐ€ํ•˜๊ธฐ

: ๋งŽ์€ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์‚ญ์ œ๋  ๋•Œ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์‚ฌ์šฉ ์ค‘์ด๋˜ ๋ฆฌ์†Œ์Šค๋ฅผ ํ™•๋ณดํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค.

  • Clock์ด ์ฒ˜์Œ DOM์— ๋ Œ๋”๋ง ๋  ๋•Œ๋งˆ๋‹คย ํƒ€์ด๋จธ๋ฅผ ์„ค์ •ย ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์ด๊ฒƒ์€ React์—์„œ โ€œmountingโ€œ์ด๋ผ๊ณ  ํ•œ๋‹ค.
  • Clock์— ์˜ํ•ด ์ƒ์„ฑ๋œ DOM์ด ์‚ญ์ œ๋  ๋•Œ๋งˆ๋‹คย ํƒ€์ด๋จธ๋ฅผ ํ•ด์ œํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์ด๊ฒƒ์€ React์—์„œ โ€œunmountingโ€œ์ด๋ผ๊ณ  ํ•œ๋‹ค.
  • ์ปดํฌ๋„ŒํŠธ ํด๋ž˜์Šค์—์„œ ํŠน๋ณ„ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋งˆ์šดํŠธ๋˜๊ฑฐ๋‚˜ ์–ธ๋งˆ์šดํŠธ ๋  ๋•Œ ์ผ๋ถ€ ์ฝ”๋“œ๋ฅผ ์ž‘๋™์‹œํ‚ฌ ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“œ๋Š” ๋ฉ”์„œ๋“œ๋“ค์„ย โ€œ์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œโ€ย ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

์ƒ์„ฑ : componentDidMount()ย ํ›…์€ ์ดˆ๊ธฐ ๋ Œ๋”๋ง ์‹œ ์‹คํ–‰ ํ•˜์ง€ ์•Š๊ณ  DOM์ด ๋ Œ๋”๋ง ๋œ ์ดํ›„ ๋™์ž‘ํ•œ๋‹ค. ์ด ๋ถ€๋ถ„์ด ํƒ€์ด๋จธ๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์ ๋‹นํ•˜๋‹ค.

1
2
3
4
5
6
componentDidMount() {
  this.timerID = setInterval(
    () => this.tick(),
    1000
  )
}
  • this (this.timerId)์—์„œ ์–ด๋–ป๊ฒŒ ํƒ€์ด๋จธ ID๋ฅผ ์ œ๋Œ€๋กœ ์ €์žฅํ•˜๋Š”์ง€ ์ฃผ์˜ํ•˜์ž.

๋งˆ์šดํŠธ ํ•ด์ œ : componentWillUnmount()ย ๋ผ์ดํ”„์‚ฌ์ดํด ํ›…์—์„œ ํƒ€์ด๋จธ๋ฅผ ์ข…๋ฃŒํ•œ๋‹ค.

1
2
3
componentWillUnmount() {
  clearInterval(this.timerId);
}

: this.setState()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ state ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = {date: new Date()};
  }

  componentDidMount() {
    this.timerID = setInterval(
      () => this.tick(),
      1000
    );
  }

  componentWillUnmount() {
    clearInterval(this.timerID);
  }

  tick() {
    this.setState({
      date: new Date()
    });
  }

  render() {
    return (
      <div>
        <h1>Hello, world!</h1>
        <h2>It is {this.state.date.toLocaleTimeString()}.</h2>
      </div>
    );
  }
}

ReactDOM.render(
  <Clock />,

1๏ธโƒฃ <Clock />์ด ReactDOM.render()์— ์ „๋‹ฌ๋  ๋•Œ, React๋Š” Clock ์ปดํฌ๋„ŒํŠธ์˜ ์ƒ์„ฑ์ž(constructor) ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. Clock์ด ํ˜„ ์‹œ๊ฐ„ ํ™”๋ฉด์— ๋ณด์—ฌ์งˆ ๋•Œ, ํ˜„ ์‹œ๊ฐ„์„ ํฌํ•จํ•˜๋Š” this.state๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ์ด state๋Š” ๋‚˜์ค‘์— ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค.

2๏ธโƒฃ React๋Š” Clock์ปดํฌ๋„ŒํŠธ์˜ render()๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. React๊ฐ€ Clock์ปดํฌ๋„ŒํŠธ์˜ render()๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. ๊ทธ๋‹ค์Œ React๋Š”ย Clockย ์˜ ๋ Œ๋”๋ง ์ถœ๋ ฅ๊ณผ ์ผ์น˜ํ•˜๋„๋ก DOM์„ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.

3๏ธโƒฃ Clockย ์ถœ๋ ฅ์ด DOM์— ์ฃผ์ž…๋˜์—ˆ์„ ๋•Œ, React๋Š”ย componentDidMount()ย ๋ผ์ดํ”„ ํ›…์„ ํ˜ธ์ถœํ•œ๋‹ค. ๊ทธ์•ˆ์—ย Clockย ์ปดํฌ๋„ŒํŠธ๋Š” ๋ธŒ๋ผ์šฐ์ €์—๊ฒŒ ์ปดํฌ๋„ŒํŠธ์˜ย tick()ย ๋ฉ”์„œ๋“œ๋ฅผ ์ดˆ๋‹น ํ•œ๋ฒˆ ํ˜ธ์ถœํ•˜๋Š” ํƒ€์ด๋จธ๋ฅผ ์„ค์ •ํ•˜๋ผ๊ณ  ์š”๊ตฌํ•œ๋‹ค.

4๏ธโƒฃ๋งค ์ดˆ๋งˆ๋‹คย ๋ธŒ๋ผ์šฐ์ €๊ฐ€tick()ย ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. ๊ทธ ์•ˆ์—์„œย Clockย ์ปดํฌ๋„ŒํŠธ๋Š” ํ˜„์žฌ ์‹œ๊ฐ„์„ ํฌํ•จํ•˜๋Š” ๊ฐ์ฒด๋กœย setState()ย ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ UI ์—…๋ฐ์ดํŠธ๋ฅผ ์˜ˆ์•ฝํ•œ๋‹ค.ย setState()ย ํ˜ธ์ถœ ๋•๋ถ„์—, React๋Š” ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋œ ๊ฑธ ์•Œ๊ณ  ์žˆ๊ณ ,ย render()ย ๋ฉ”์„œ๋“œ๋ฅผ ๋‹ค์‹œ ํ•œ๋ฒˆ ํ˜ธ์ถœํ•ด ํ™”๋ฉด์— ๋ฌด์—‡์ด ์žˆ์–ด์•ผ ํ•˜๋Š” ์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ด ๋•Œ ย render()ย ๋ฉ”์„œ๋“œ ๋‚ด์˜ย this.state.dateย ๊ฐ€ ๋‹ฌ๋ผ์ง€๊ณ  ๋ Œ๋”๋ง ์ถœ๋ ฅ๊ฐ’์€ ์—…๋ฐ์ดํŠธ๋œ ์‹œ๊ฐ์„ ํฌํ•จํ•œ๋‹ค. React๋Š” ๊ทธ์— ๋”ฐ๋ผ DOM์„ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.

5๏ธโƒฃ๋งŒ์•ฝย Clockย ์ปดํฌ๋„ŒํŠธ๊ฐ€ DOM์—์„œ ์‚ญ์ œ๋˜์—ˆ๋‹ค๋ฉด, React๋Š”ย componentWillUnmount()ย ๋ผ์ดํ”„์‚ฌ์ดํด ํ›…์„ ํ˜ธ์ถœํ•˜๊ณ  ํƒ€์ด๋จธ๋ฅผ ๋ฉˆ์ถ˜๋‹ค.

State ๋ฐ”๋ฅด๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ

: setState()์— ๋Œ€ํ•ด ์•Œ์•„์•ผ ํ•  3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

  • State๋ฅผ ์ง์ ‘ ์ˆ˜์ •ํ•˜์ง€ ๋ง์ž

    1
    2
    3
    4
    5
    
    // Wrong โŒ
    this.state.comment = "Hello";
    
    // Correct โญ•
    this.setState({ comment: "Hello" });
    
    • this.state๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์ผํ•œ ๊ณณ์€ constructor๋‚ด๋ถ€์ด๋‹ค.
  • State ์—…๋ฐ์ดํŠธ๋Š” ๋น„๋™๊ธฐ์ผ ์ˆ˜ ์žˆ๋‹ค.

    • React๋Š” ์—ฌ๋Ÿฌ setState()ํ˜ธ์ถœ์„ ์„ฑ๋Šฅ์„ ์œ„ํ•ด ๋‹จ์ผ ์—…๋ฐ์ดํŠธ๋กœ ํ•œ๊บผ๋ฒˆ์— ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.
    • this.props๋ฐ this.state๊ฐ€ ๋น„๋™๊ธฐ๋กœ ์—…๋ฐ์ดํŠธ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋‹ค์Œ state๋ฅผ ๊ณ„์‚ฐํ•  ๋•Œ ํ•ด๋‹น ๊ฐ’์„ ์‹ ๋ขฐํ•ด์„œ๋Š” ์•ˆ๋œ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด, ์นด์šดํ„ฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ์ด ์ฝ”๋“œ๋Š” ์‹คํŒจํ•  ์ˆ˜ ์žˆ๋‹ค.
    1
    2
    3
    4
    
    // Wrong โŒ
    this.setState({
      counter: this.state.counter + this.props.increment,
    });
    
    • ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋‹ค๋ฅธ ํ˜•ํƒœ์˜ setState()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๊ทธ ํ•จ์ˆ˜๋Š” ์ด์ „ state๋ฅผ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฐ›์•„๋“œ๋ฆด ๊ฒƒ์ด๊ณ , ์—…๋ฐ์ดํŠธ๊ฐ€ ์ ์šฉ๋œ ์‹œ์ ์˜ props๋ฅผ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฐ›์•„๋“ค์ผ ๊ฒƒ์ด๋‹ค.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    // Correct - ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ โญ•
    this.setState((prevState, props) => ({
      counter: prevState.counter + props.increment,
    }));
    
    // Correct - ์ผ๋ฐ˜ ํ•จ์ˆ˜ โญ•
    this.setState(function (prevState, props) {
      return {
        counter: prevState.counter + props.increment,
      };
    });
    
  • State ์—…๋ฐ์ดํŠธ๋Š” ๋ณ‘ํ•ฉ๋œ๋‹ค.

    • setState()๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ, React๋Š” ํ˜„์žฌ state์™€ ์ œ๊ณตํ•œ ๊ฐ์ฒด๋ฅผ ๋ณ‘ํ•ฉํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, state๋Š” ์—ฌ๋Ÿฌ ๋…๋ฆฝ์ ์ธ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
    1
    2
    3
    4
    5
    6
    7
    
    constructor(props) {
      super(props);
      this.state = {
        posts: [],
        comments: []
      };
    }
    
    • ๊ฐœ๋ณ„ setState()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์•„์ดํ…œ์„ ๊ฐ ๊ฐ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ๋‹ค.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    componentDidMount() {
      fetchPosts().then(response => {
        this.setState({
          posts: response.posts
        });
      });
    
      fetchComments().then(response => {
        this.setState({
          comments: response.comments
        });
      });
    }
    
    • ๋ณ‘ํ•ฉ์€ ์–•๊ฒŒ ์ด๋ฃจ์–ด์ ธ์„œย this.setState({comments})๋Š”ย this.state.posts์— ์˜ํ–ฅ์„ ์ฃผ์ง„ ์•Š์ง€๋งŒย this.state.comments๋Š” ์™„์ „ํžˆ ๋Œ€์ฒด๋œ๋‹ค.

๋ฐ์ดํ„ฐ๋Š” ์•„๋ž˜๋กœ ํ๋ฅธ๋‹ค.

: ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋‚˜ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋Š” ํŠน์ • ์ปดํฌ๋„ŒํŠธ์˜ state ์œ ๋ฌด๋ฅผ ์•Œ ์ˆ˜ ์—†์œผ๋ฉฐ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํ•จ์ˆ˜๋‚˜ ํด๋ž˜์Šค๋กœ ์„ ์–ธ๋˜์—ˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์—†๋‹ค.
์ด๋Š” state๊ฐ€ ๋กœ์ปฌ์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ฑฐ๋‚˜ ์บก์Šํ™”๋œ ์ด์œ ์ด๋‹ค. ์ปดํฌ๋„ŒํŠธ ์ž์‹  ์™ธ์—๋Š” ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค. ์ปดํฌ๋„ŒํŠธ๋Š” ์ž์‹ ์˜ state๋ฅผ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์— props๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

1
<FormattedDate date={this.state.date} />

FormattedDateย ์ปดํฌ๋„ŒํŠธ๋Š” props์—์„œย date๋ฅผ ๋ฐ›์ง€๋งŒ ์ด ๊ฐ’์ดย Clock์˜ state์ธ ์ง€,ย Clock์˜ props์ธ ์ง€, ํ˜น์€ ์ˆ˜๋™์œผ๋กœ ์ž…๋ ฅํ•œ ๊ฑด์ง€ ์•Œ ์ˆ˜ ์—†๋‹ค.

1
2
3
function FormattedDate(props) {
  return <h2>It is {props.date.toLocaleTimeString()}.</h2>;
}
  • ์ด๋Ÿฐ ๋ฐ์ดํ„ฐ ํ๋ฆ„์„ ๋ณดํ†ต โ€œํ•˜ํ–ฅ์‹โ€ ํ˜น์€ โ€œ๋‹จ๋ฐฉํ–ฅ์‹โ€ ๋ฐ์ดํ„ฐ ํ๋ฆ„์ด๋ผ๊ณ  ํ•œ๋‹ค. ๋ชจ๋“  state๋Š” ํ•ญ์ƒ ํŠน์ • ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์†Œ์œ ํ•˜๋ฉฐ, ๊ทธ state์—์„œ ํŒŒ์ƒ๋œ ๋ชจ๋“  UI, ๋ฐ์ดํ„ฐ๋Š” ํŠธ๋ฆฌ์˜ ์ปดํฌ๋„ŒํŠธ โ€œ์•„๋ž˜โ€์—๋งŒ ์˜ํ–ฅ์„ ๋ผ์นœ๋‹ค.

: ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์™„์ „ํžˆ ๋…๋ฆฝ์ ์ด๋ผ๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด App ์„ ๋ Œ๋”๋งํ•˜๋Š” ์„ธ ๊ฐœ์˜ <Clock> ์„ ์ƒ์„ฑํ•œ๋‹ค.

1
2
3
4
5
6
7
8
9
function App() {
  return (
    <div>
      <Clock />
      <Clock />
      <Clock />
    </div>
  );
}
  • ๊ฐ <Clock>์€ ์ž์‹ ๋งŒ์˜ ํƒ€์ด๋จธ๋ฅผ ์„ค์ •ํ•˜๊ณ  ๋…๋ฆฝ์ ์œผ๋กœ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค.
    ๐Ÿ‘‰๐Ÿป React์•ฑ์—์„œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ƒํƒœ๊ฐ€ ์žˆ๋Š” ์ง€ ์—†๋Š”์ง€์— ๋Œ€ํ•œ ๊ฒƒ์€ ์‹œ๊ฐ„์ด ์ง€๋‚จ์— ๋”ฐ๋ผ ๋ณ€๊ฒฝ๋ ์ˆ˜ ์žˆ๋Š” ๊ตฌํ˜„ ์„ธ๋ถ€ ์‚ฌํ•ญ์œผ๋กœ ๊ฐ„์ฃผ๋œ๋‹ค. ์œ ์ƒํƒœ ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ ๋ฌด์ƒํƒœ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ , ๊ทธ ๋ฐ˜๋Œ€ ๊ฒฝ์šฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ด๋‹ค.

Tags:

Categories:

Updated:

Leave a comment