Session 13 . How to update UI (state, event)

๊น€๋ฏผ์žฌยท2021๋…„ 9์›” 6์ผ
1

TIL, WeCode, Courseย 

๋ชฉ๋ก ๋ณด๊ธฐ
29/48
post-thumbnail

*๐Ÿ”Study Keyword :

1>๐Ÿ”‘Stete์™€ ๐Ÿ”‘event๋ฅผ ์‚ฌ์šฉํ•ด์„œ UI๋ฅผ ์—…๋ฐ์ดํŠธ ํ•ด๋ณด๊ณ  2>๐Ÿ’ปProps๋กœ state์™€ event handler๋ฅผ ๋„˜๊ฒจ๋ณด์ขŒ.

1. State

  • state๋ž€ ํ™”๋ฉด์— ๋ณด์—ฌ์ง€๋Š” ๊ฒƒ์˜ ํ•ต์‹ฌ์ •๋ณด๋กœ์„œ ๊ฒฐ๊ตญ UI๋Š” render(state)์ด๋‹ค.
  • ์ด๋Š” ํ™”๋ฉด์— ๋ณด์—ฌ์ง€๋Š” UI๋Š” ๋ชจ๋‘ state๋กœ๋ถ€ํ„ฐ ๊ทธ๋ ค์ง€๊ณ  ์˜ค์ง state์— ์˜ํ•ด์„œ๋งŒ ํ™”๋ฉด์ด ๋ณ€ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • state์˜ ๋‹จ์–ด ๋œป์ธ ์ƒํƒœ, ์ฆ‰ ์˜๋ฏธ ๊ทธ๋Œ€๋กœ ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ๊ฐ’์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

  • ์ฆ‰, state๋Š” ํ™”๋ฉด์— ๋ณด์—ฌ์ค„ ์ปดํฌ๋„ŒํŠธ์˜ UI์ •๋ณด๋ฅผ ์ง€๋‹ˆ๊ณ  ์žˆ๋Š” "๊ฐ์ฒด"์ด๋‹ค.

1_1. State ๊ฐ์ฒด

  • ์ค‘์š”ํ•œ ๊ฑด State ์—ญ์‹œ props์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ์ฒด๋ผ๋Š” ์ ์ด๋‹ค.
<script>
    this.state = {
      color: 'red',
      fontSize: '40px'
    };
</script>
  • ๊ฐ์ฒด ๋‚ด๋ถ€์˜ key(color)์™€ key์˜ value(red)๊ฐ€ ํ•œ ์Œ์œผ๋กœ ๋“ค์–ด๊ฐ„๋‹ค.

1_2. state ์„ค์ •์ˆœ์„œ

  • ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„  render ํ•จ์ˆ˜๊ฐ€ ํ•„์ˆ˜์ด๋ฉฐ renderํ•จ์ˆ˜ ์•ˆ์— ํ™”๋ฉด์— ๋ณด์—ฌ์ง€๊ณ ์ž ํ•˜๋Š” JSX์š”์†Œ๊ฐ€ return๋ฌธ ์•ˆ์— ๋“ค์–ด๊ฐ„๋‹ค.

1> constructor ํ•จ์ˆ˜ ๋‚ด๋ถ€์— super()๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  props๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌํ•ด์ค€๋‹ค.
2> ์ดํ›„ this.state ๊ฐ’์— ์ดˆ๊ธฐ๊ฐ’(์ปดํฌ๋„ŒํŠธ์˜ ์ดˆ๊ธฐ ์ƒํƒœ ๊ฐ’)์„ ๋จผ์ € ์„ธํŒ…ํ•ด์ฃผ๋ฉด ์ดํ›„ 3>render ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋œ๋‹ค.
4> state์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” this.setState()๋ฅผ ํ†ตํ•ด ๋ณ€๊ฒฝํ•ด์•ผํ•˜๋ฉฐ,
5> ์ด๋ฅผ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ์˜ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ์ด ๋˜๋ฉด render ํ•จ์ˆ˜๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง, ์žฌ์‹คํ–‰ ๋œ๋‹ค.

1_3. State ์‚ฌ์šฉ ์˜ˆ์‹œ

  • state์—์„œ ์ƒํƒœ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” ์ด์œ ๋Š” ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€ ์š”์†Œ์—์„œ ์ƒํƒœ๊ฐ’์„ ๋ฐ˜์˜ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ํšจ๊ณผ์ ์œผ๋กœ ํ™”๋ฉด(UI)์— ๋‚˜ํƒ€๋‚˜๋„๋ก ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค.
  • ์ด๋Š” ์•ž์„œ ์‚ดํŽด๋ณธ UI = render(state) ๊ณต์‹๊ณผ ๋™์ผํ•˜๋‹ค.
  • ํ•ด๋‹น state๋ฅผ ํ™œ์šฉํ•˜์—ฌ JSX ์š”์†Œ์— ์ธ๋ผ์ธ ์Šคํƒ€์ผ์„ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
<script>
import React, { Component } from 'react';
class State extends Component {
  constructor() {
    super();
    this.state = {
      color: 'red',
      fontSize: '40px'
    };
  }
  render() {
    return (
      <div>
        <h1 style={{ color: this.state.color, fontSize: this.state.fontSize }}>
          Class Component | State
        </h1>
      </div>
    );
  }
}
export default State;
</script>
  • dot notation์„ ์ด์šฉํ•˜์—ฌ ๊ฐ์ฒด์˜ ํŠน์ • key ๊ฐ’์— ์ ‘๊ทผํ•˜์—ฌ ํŠน์ • key ๊ฐ’์„ color ์†์„ฑ์˜ value๋กœ ์ง€์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค.
  • ์ด๋ฅผ ํ†ตํ•ด state ์ƒํƒœ ๊ฐ’์„ ์„ค์ •ํ•˜๋ฉด ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์˜ ์š”์†Œ(h1)์—์„œ ์ƒํƒœ๊ฐ’์„ ๋ฐ˜์˜ํ•˜์—ฌ UI์— ๋‚˜ํƒ€๋‚˜์ง€๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.
<script>
  <h1 style={{ color: this.state.color, fontSize: this.state.fontSize }}>
   Class Component | State
  </h1>
// this : ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
// this.state : ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์˜ state ๊ฐ์ฒด๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
// this.state.color : state ๊ฐ์ฒด์˜ ํŠน์ • key ๊ฐ’์ด๋‹ค.
</script>

2. Event & setState

<script>
setState(updater, [callback])
</script>
  • setState ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด state ์ƒํƒœ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

  • setState ํ•จ์ˆ˜๋Š” 2๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ›๋Š”๋ฐ
    1> ์ฒซ ๋ฒˆ์งธ updater์—๋Š” ๊ฐ์ฒด๊ฐ€ ๋“ค์–ด๊ฐ€๊ฑฐ๋‚˜ ํ•จ์ˆ˜ํ˜•ํƒœ๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.
    2> ๋‘๋ฒˆ์งธ๋Š” callback ์ธ์ž์—๋Š” ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

<script>
import React, { Component } from 'react';
class State extends Component {
  constructor() {
    super();
    this.state = {
      color: 'red',
    };
  }
  handleColor = () => {
    this.setState({
      color: 'blue'
    })
  }
  render() {
    return (
      <div>
        <h1 style={{ color: this.state.color }}>Class Component | State</h1>
        <button onClick={this.handleColor}>Click</button>
      </div>
    );
  }
}
export default State;</script>
</script>
  • 1> button ์š”์†Œ์— onClick ์ด๋ฒคํŠธ ๋ฐœ์ƒ์‹œ์— ํ˜„์žฌ ์ปดํฌ๋„ŒํŠธ(state)์˜ handleColor ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋œ๋‹ค.
  • 2> handleColor ํ•จ์ˆ˜ ์‹คํ–‰๋˜๋ฉด setState ํ•จ์ˆ˜ ์‹คํ–‰๋˜์–ด ์ด๋ฅผ ํ†ตํ•ด state์˜ color ๊ฐ’์„ 'blue' ๋กœ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค.
  • 3> state ๊ฐ’์ด ๋ฐ”๋€Œ๋ฉด render ํ•จ์ˆ˜๊ฐ€ ๋‹ค์‹œ ํ˜ธ์ถœ๋˜์–ด ๋ฐ”๋€ state ๊ฐ’์„ ๋ฐ˜์˜ํ•˜์—ฌ <h1> ํƒœ๊ทธ ๊ธ€์ž ์ƒ‰์ƒ ๋ณ€๊ฒฝ๋œ๋‹ค.

โœจํ•„์ˆ˜โœจ) state์— ๋”ฐ๋ผ ๋ณ€ํ•˜๋Š” ์œ ๋™์  className ์ง€์ •ํ•˜๊ธฐ

  • ์ธ๋ผ์ธ ์š”์†Œ์— ์Šคํƒ€์ผ์„ ์ง€์ •ํ•˜์—ฌ ๋ณ€๊ฒฝํ•˜๋Š” ์‹์˜ ์ฝ”๋“œ๋Š” ์œ ์ง€๋ณด์ˆ˜์— ์ข‹์ง€์•Š๋‹คใ…œ๐Ÿ˜ฃ
  • ๋”ฐ๋ผ์„œ state์— ๋”ฐ๋ผ className์„ ๋ณ€๊ฒฝํ•˜์—ฌ style์„ ๋ฐ”๊พธ์–ด์ฃผ๋„๋ก ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
<script>
import React, { Component } from 'react';
class State extends Component {
  constructor() {
    super();
    this.state = {
      isChangecolor: false,
    };
  }
  handleColor = () => {
    this.setState({
      isChangecolor: !this.state.isChangecolor,
    })
  }
  render() {
    return (
      <div>
        <h1 className={isChangecolor ? "red" : "normal "}>
          Class Component | State
        </h1>
        <button onClick={this.handleColor}>Click</button>
      </div>
    );
  }
}
export default State
</script>

3. Props๋กœ state์™€ event handler ๋„˜๊ธฐ๊ธฐ

  • props(properties, ์†์„ฑ)๋Š” ๋‹จ์–ด ์˜๋ฏธ ๊ทธ๋Œ€๋กœย ์ปดํฌ๋„ŒํŠธ์˜ ์†์„ฑ๊ฐ’์œผ๋กœ
  • props๋ž€ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€๋‹ˆ๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
  • props๋Š” ๋ถ€๋ชจ์˜ state๋ฅผ ์ž์‹์—๊ฒŒ ๋„˜๊ฒจ์ฃผ๋Š” ํ˜•ํƒœ์ด๋‹ค. ์ด๋ ‡๊ฒŒ ๋ถ€๋ชจ์˜ ์ƒํƒœ๋ฅผ ์ž์‹์—๊ฒŒ ๋„˜๊ฒจ์ฃผ๋Š” ํ˜•ํƒœ๋Š” ๋ฆฌ์•กํŠธ์—์„œ ์ž์ฃผ ์“ฐ์ด๋Š” ํŒจํ„ด์ด๋‹ค.
<script>
import React from 'react';
import Child from '../pages/Child/Child';
// ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ
class Parent extends React.Component {
  constructor() {
    super();
    this.state = {
      color: 'red'
    };
  }
  render() {
    return (
      <div>
        <h1>Parent Component</h1>
       // ์ž์‹ ์ปดํฌ๋„ŒํŠธ์˜ props ์„ค์ • ํ›„ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ state๋ฅผ ๋„˜๊ฒจ์ฃผ๊ณ  ์žˆ๋‹ค.
        <Child titleColor={this.state.color}/>
      </div>
    );
  }
}
export default Parent;
</script>
<script>
import React from 'react';
// ์ž์‹ ์ปดํฌ๋„ŒํŠธ
class Child extends React.Component {
  render() {
         // ๋„˜๊ฒจ๋ฐ›์€ state๋ฅผ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ฐ์–ด๋ณธ๋‹ค.
		console.log("์ด๊ฐ’์€๋ญ˜๊นŒ?", this.props.titleColor);
        // ๋ถ€๋ชจ์˜ state ๊ฐ’์ธ 'red'๊ฐ€ ์งœ์ž”!
    return (
      <div>
        <h1 style={{color : this.props.titleColor}}>Child Component</h1>
      </div>
    );
  }
}
export default Child;
</script>
  • ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋Š” ๋ถ€๋ชจ๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋กœ๋ถ€ํ„ฐ ํ™”๋ฉด์„ ๋‹ฌ๋ผ์ง€๊ฒŒ ํ•ด์ค€๋‹ค.
  • ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ๋„˜๊ฒจ์ฃผ๋Š” ๋ฐ์ดํ„ฐ๋Š” state๊ฐ€ ๋  ์ˆ˜ ์žˆ๊ณ  ํ•จ์ˆ˜๊ฐ€ ๋  ์ˆ˜ ์žˆ์–ด ๊ทธ ํ•จ์ˆ˜๊ฐ€ event handler์ผ ์ˆ˜ ๋„ ์žˆ๋‹ค.
  • props๋Š” ์‹ค์ œ๋กœ state๋กœ๋ถ€ํ„ฐ ์˜ค๊ณ  ์žˆ๋‹ค.
  • props ๋ช…๊ณผ event handler๋กœ ๋„˜๊ฒจ์ค„ ํ•จ์ˆ˜ ๋ช…์„ ๋˜‘๊ฐ™์€ ๊ฑธ๋กœ ์ง€์–ด์ฃผ๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ธ ์ปจ๋ฒค์…˜์ด๋‹ค.

์ž์‹ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ถ€๋ชจ์˜ ์ปดํฌ๋„ŒํŠธ์˜ state๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฒ•

  • 1>๋ถ€๋ชจ๊ฐ€ ์ž์‹์—๊ฒŒ ๋ถ€๋ชจ์˜ state๋ฅผ ๋ณ€ํ™”์‹œํ‚ค๋Š” event handler ํ•จ์ˆ˜๋ฅผ props๋กœ ๋„˜๊ฒจ์ค€๋‹ค๋ฉด, 2>์ž์‹์€ ๊ทธ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๋ถ€๋ชจ์˜ state๋ฅผ ๋ณ€ํ™”์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค!
<script>
import React from 'react';
import Child from '../pages/Children/Children';
class Parent extends React.Component {
  constructor() {
    super();
    this.state = {
      color: 'red'
    };
  }
  changeColor = () => {
    this.setState({
      color: 'blue'
    })
  }
  render() {
    return (
      <div>
        <h1>Parent Component</h1>
				<Child titleColor={this.state.color} changeColor={this.changeColor}/>
      </div>
    );
  }
}
export default State;
</script>
  • ์ž์‹์˜ ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์œผ๋กœ ์ด๋ฒคํŠธ ํ•จ์ˆ˜๋ฅผ ๋„˜๊ฒจ์ฃผ๊ณ 
  • ์ž์‹์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” props๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๋ Œ๋”๊ฐ€ ๋ฐ”๋€Œ๊ธฐ ๋•Œ๋ฌธ์— ์ž์‹ ๋ถ€๋ถ„๋„ ๋ Œ๋”๋œ๋‹ค.
  • ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ props ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด state ๋ฐ์ดํ„ฐ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ถ€๋ชจ์—์„œ ์ •์˜ํ•œ event handler ํ•จ์ˆ˜๋„ ๋„˜๊ฒจ์ค€๋‹ค.
  • props์˜ changeColor ๊ฐ’์œผ๋กœ Parent ํ•จ์ˆ˜์—์„œ ์ •์˜ํ•œ handleColor ํ•จ์ˆ˜ ์ž์ฒด๋ฅผ ๋„˜๊ฒจ์ฃผ๊ณ  ์žˆ๋‹ค.
  • <Child /> ์ปดํฌ๋„ŒํŠธ์—์„œ ์–ด๋–ป๊ฒŒ props๋กœ ์ „๋‹ฌ๋ฐ›์€ handleColor ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
  • state๋Š” ๋‹จ๋ฐฉํ–ฅ์œผ๋กœ ํ˜๋Ÿฌ๊ฐ„๋‹ค

  • ๋”ฐ๋ผ์„œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ ์…‹์Šคํ…Œ์ดํŠธ๋กœ ์Šคํ…Œ์ดํŠธ๋ฅผ ๋ณ€๊ฒฝํ•œ๋‹ค๋Š” ๊ฑด ์ž์‹์ปดํฌ๋„ŒํŠธ ์ž์ฒด๊ฐ€ ๊ฐ€์ง„ ์Šคํ…Œ์ดํŠธ๋ฅผ ์—…๊ทธ๋ ˆ์ด๋“œํ•˜๋Š” ๊ฒƒ์ด์ง€ ๋ถ€๋ชจ๋กœ๋ถ€ํ„ฐ ๋ฐ›์•„์˜จ ๊ฒƒ์„ ์—…๋ฐ์ดํŠธํ•˜์ง€ ๋ชปํ•œ๋‹ค.

*๐Ÿ’กconclusion

  • state์™€ event๋ฅผ ํ™œ์šฉํ•˜์—ฌ UI๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ๊ณผ์ •์„ ์ดํ•ดํ•ด๋ณด๊ณ  UI = render(state) ๊ณต์‹์˜ ์˜๋ฏธ๋ฅผ ๊ณฑ์”น์–ด๋ณด์ž!

# ๐Ÿ“‘Study Source

  • ์œ„์ฝ”๋“œ ์žฅํ˜„๋‹˜์˜ ํฌ์ธํŠธ ๊ฐ•์˜ ์ค‘ :]
profile
์ž๊ธฐ ์‹ ๋ขฐ์˜ ํž˜์„ ๋ฏฟ๊ณ  ์‹ค์ฒœํ•˜๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๊ณ ์žํ•ฉ๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€