๐Ÿ‡Basic React Part 1 - Counter ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ

๐Ÿ”ขCounter

๋ฌด์–ธ๊ฐ€ ์ˆซ์ž๋ฅผ ํ•˜๋‚˜ ์˜ฌ๋ฆฌ๊ณ  ๋”ํ•˜๋Š” ๊ธฐ๋Šฅ์€ ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ผ๋“ ์ง€ ๋งŽ์ด ์“ธ๋ฒ•ํ•œ ๊ธฐ๋Šฅ์ด๋‹ค.

๊ทธ๋ž˜์„œ ์‹ค์Šตํ•ด ๋ณด์•˜๋‹ค.

๐Ÿš€Code (1) - Class Component

import React, { Component } from 'react'

class Counter extends Component {
  constructor(props) {
    super(props)
    this.state = {
      count: 1,
    }
  }
  render() {
    const { count } = this.state
    return (
      <>
        <h1>
          {count === 0
            ? `๋” ์ด์ƒ ${count} ์—์„œ ๋บ„ ์ˆ˜ ์—†์–ด์š”.`
            : `ํ˜„์žฌ ๊ฐœ์ˆ˜๋Š” โฉ ${count}`}
        </h1>
        <button onClick={() => this.setState({ count: count + 1 })}>Add</button>
        <button
          onClick={() => {
            if (this.state.count !== 0) {
              this.setState(
                prevState => ({ count: prevState.count - 1 }),
                () => console.log('๋ฐฉ๊ธˆ setState ๊ฐ€ ํ˜ธ์ถœ๋˜์–ด ํ•˜๋‚˜ ๋บ์Šต๋‹ˆ๋‹ค.')
              )
            }
          }}
        >
          Subtract
        </button>
      </>
    )
  }
}

export default Counter

ํด๋ž˜์Šคํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ state ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์€ this.state ๋กœ ์ ‘๊ทผํ•ด์„œ ๊ฐ€์ ธ์˜ค๊ฒŒ ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  render ๋‚ด์— ์ƒํƒœ๋ฅผ ์ „๋‹ฌํ•˜๊ฑฐ๋‚˜ jsx ๋‚ด์— ๋ณด์—ฌ์ง€๊ฒŒ ํ• ํ…๋ฐ, ๋งค๋ฒˆ this.state.count, this.state.movies โ€ฆ ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง„๋‹ค.

๊ทธ๋ž˜์„œ ์ด ๋˜ํ•œ ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น์„ ํ†ตํ•ด state ๋ณ€์ˆ˜๋ฅผ ๋ฏธ๋ฆฌ ๊ฐ€์ ธ์™€ ์„ ์–ธํ•ด ๋†“์œผ๋ฉด ์ฝ”๋“œ๊ฐ€ ์งง์•„์ง€๊ณ  ๊ฐ€์ ธ๋‹ค ์“ฐ๊ธฐ ์ข‹๋‹ค.

const { count } = this.state

๋˜ํ•œ setState ๋ฅผ ํ˜ธ์ถœ ํ•  ๋•Œ, ๋‚ด๋ถ€ ์ธ์ž๋กœ ๊ฐ์ฒด ์ž์ฒด๊ฐ€ ์˜ฌ ์ˆ˜๋„ ์žˆ๊ณ  ์•„๋‹˜ ํ•จ์ˆ˜๊ฐ€ ์˜ฌ ์ˆ˜๋„ ์žˆ๋‹ค.

๊ฐ์ฒด๊ฐ€ ์˜ฌ ๋•Œ๋Š” ๋งŒ์•ฝ state ๊ฐ€ ๋ฐฐ์—ด๋กœ ๋ฐ›๋Š”๋‹ค ๊ฐ€์ •ํ•˜๋ฉด Spread Operator ๋ฅผ ํ†ตํ•ด ์ด์ „ ๋ฐฐ์—ด์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•ด ๋‚˜๊ฐˆ ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

๋‚ด๋ถ€ ์ธ์ž๋กœ ํ•จ์ˆ˜๊ฐ€ ์˜ฌ ๋•Œ๋Š” prevState ๋ฅผ ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฐ›๋Š” ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ์ž‘์„ฑ๋œ๋‹ค.

if (this.state.count !== 0) {
  this.setState(
    prevState => ({ count: prevState.count - 1 }),
    () => console.log('๋ฐฉ๊ธˆ setState ๊ฐ€ ํ˜ธ์ถœ๋˜์–ด ํ•˜๋‚˜ ๋บ์Šต๋‹ˆ๋‹ค.')
  )
}

์˜ต์…˜์œผ๋กœ๋‹ค๊ฐ€ ๋‘๋ฒˆ์งธ ์ธ์ž์— ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹คํ–‰ํ•  ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

๐Ÿš€Code (2) - Functional Component

์•„์ง hooks ์— ๋Œ€ํ•ด ์ œ๋Œ€๋กœ ์•Œ์ง€๋Š” ๋ชปํ•˜์ง€๋งŒ ์ผ๋‹จ ์•Œ๊ณ  ์žˆ๋Š” ์„ ์—์„œ ํ•œ๋ฒˆ ์จ๋ดค๋‹ค.

import React, { useState } from 'react'

const CounterFunc = () => {
  const [count, setCount] = useState(1)
  return (
    <>
      <h1>
        {count === 0
          ? `๋” ์ด์ƒ ${count} ์—์„œ ๋บ„ ์ˆ˜ ์—†์–ด์š”.`
          : `ํ˜„์žฌ ๊ฐœ์ˆ˜๋Š” โฉ ${count}`}
      </h1>
      <button onClick={() => setCount(count + 1)}>Add</button>
      <button
        onClick={() => {
          if (count !== 0) {
            setCount(count - 1)
          }
        }}
      >
        Subtract
      </button>
    </>
  )
}

export default CounterFunc

๐Ÿง๋งŒ๋“ค์–ด๋ณธ ๋ชจ์Šต

counter


Written by@[DotoriMook]
ํ”„๋ก ํŠธ์—”๋“œ ์ฃผ๋‹ˆ์–ด ๊ฐœ๋ฐœ์ž ๋„ํ† ๋ฆฌ๋ฌต์˜ ๊ธฐ์ˆ ๊ฐœ๋ฐœ ๋ธ”๋กœ๊ทธ ์ž…๋‹ˆ๋‹ค.

GitHubMediumTwitterFacebook