๐Ÿ‡Basic React Part 2 - on ์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ด๋ฒคํŠธ ์—ฐ์Šต(1)

๐ŸŠClass ์ปดํฌ๋„ŒํŠธ์—์„œ์˜ Event - onXXXX ์— ํ•จ์ˆ˜ ์ง์ ‘ ์ „๋‹ฌ

input ํƒœ๊ทธ์— onChange ์ด๋ฒคํŠธ ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ์‹คํ–‰ ํ•˜๋ฉด์„œ setState ๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ํ•˜์—ฌ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝ์‹œํ‚จ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ input ์˜ value ์†์„ฑ์— this.state.message ๋ฅผ ํ‘œ๊ธฐํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š”์ง€๋Š” ์•„์ง ํ™•์ธ ์ง„ํ–‰ ์ค‘์ด๋‹ค.

button ํƒœ๊ทธ์—๋Š” onClick ์ด๋ฒคํŠธ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•ด์„œ message ์˜ ์ƒํƒœ๋ฅผ ๋น„์šฐ๊ณ  input ์— id ๋ฅผ ์ค˜์„œ, DOM ์š”์†Œ ๋ฅผ ์„ ํƒํ•˜๊ฒŒ ํ•ด์„œ

์ž…๋ ฅ๊ฐ’์„ ์ดˆ๊ธฐํ™”ํ•˜๊ฒŒ ํ–ˆ๋‹ค.

import React, { Component } from 'react'

class EventPractice extends Component {
  constructor(props) {
    super(props)
    this.state = {
      message: '',
    }
  }
  render() {
    return (
      <div>
        <h1>๋‚ด์ธ์ƒ์˜ ์ด๋ฒคํŠธ๋Š” ์–ธ์ œ์˜ค๋‚˜</h1>
        <input
          id="msg"
          type="text"
          name="message"
          value={this.state.message}
          placeholder="์ƒˆํ•ด์†Œ๋ง์„์ž…๋ ฅํ•˜์„ธ์š”"
          onChange={e => this.setState({ message: e.target.value })}
        />
        <button
          onClick={() => {
            alert(this.state.message)
            document.querySelector('#msg').value = ''
            this.setState({ message: '' })
          }}
        >
          ํ™•์ธ
        </button>
      </div>
    )
  }
}

export default EventPractice

๐ŸŠClass ์ปดํฌ๋„ŒํŠธ์—์„œ์˜ Event - ํ•จ์ˆ˜๋ฅผ ๋ฏธ๋ฆฌ ์ค€๋น„ (์ž„์˜ ๋ฉ”์„œ๋“œ ๋งŒ๋“ค๊ธฐ)

ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ this ๋Š” ํ˜ธ์ถœ๋ถ€์— ๋”ฐ๋ผ ๊ฒฐ์ •๋˜๋ฏ€๋กœ, ํด๋ž˜์Šค์˜ ์ž„์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ํŠน์ • HTML ์š”์†Œ์˜ ์ด๋ฒคํŠธ๋กœ ๋“ฑ๋ก๋˜๋Š” ๊ณผ์ •์—์„œ ๋ฉ”์„œ๋“œ์™€ this ์˜ ๊ด€๊ณ„๊ฐ€ ๋Š์–ด์ ธ ๋ฒ„๋ฆฐ๋‹ค๊ณ  ํ•œ๋‹ค.

์ด ๋•Œ๋ฌธ์— ์ž„์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์ด๋ฒคํŠธ๋กœ ๋“ฑ๋ก๋˜์–ด๋„ this ๋ฅผ ์ปดํฌ๋„ŒํŠธ ์ž์‹ ์œผ๋กœ ์ œ๋Œ€๋กœ ๊ฐ€๋ฆฌํ‚ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ this ์™€ ๋ฐ”์ธ๋”ฉ (binding) ํ•˜๋Š” ์ž‘์—…์ด ํ•„์š”ํ•˜๋‹ค.

import React, { Component } from 'react'

class EventPractice extends Component {
  constructor(props) {
    super(props)
    this.state = {
      message: '',
    }
    this.handleChange = this.handleChange.bind(this)
    this.handleClick = this.handleClick.bind(this)
  }
  handleChange(event) {
    this.setState({
      message: event.target.value,
    })
  }

  handleClick() {
    alert(this.state.message)
    document.querySelector('#msg').value = ''
    this.setState({
      message: '',
    })
  }
  render() {
    return (
      <div>
        <h1>๋‚ด์ธ์ƒ์˜ ์ด๋ฒคํŠธ๋Š” ์–ธ์ œ์˜ค๋‚˜</h1>
        <input
          id="msg"
          type="text"
          name="message"
          value={this.state.message}
          placeholder="์ƒˆํ•ด์†Œ๋ง์„์ž…๋ ฅํ•˜์„ธ์š”"
          onChange={this.handleChange}
        />
        <button onClick={this.handleClick}>ํ™•์ธ</button>
      </div>
    )
  }
}

export default EventPractice

prac event

๋งŒ์•ฝ ๋ฐ”์ธ๋”ฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด this ๊ฐ€ undefined ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋œ๋‹ค.

๋‚ด๊ฐ€ ํ˜„์žฌ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋Š” constructor ํ•จ์ˆ˜์—์„œ ํ•จ์ˆ˜๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•˜๋Š” ์ž‘์—…์ด ์ด๋ฃจ์–ด์ง€๊ณ  ์žˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ bind ๋ฅผ ์จ์ฃผ๊ธฐ๊ฐ€ ๋„ˆ๋ฌด ๊ท€์ฐฎ๊ณ  ์‹ซ๋‹ค๋ฉด?

๋ฐ”๋กœ ์†Œ๊ฐœํ•œ ์ฒซ๋ฒˆ์งธ ๋ฐฉ์‹์œผ๋กœ ์ด๋ฒคํŠธ ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ์ต๋ช…ํ•จ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ ๋” ์›์ดˆ์ ์ธ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

๐ŸŠClass ์ปดํฌ๋„ŒํŠธ์—์„œ์˜ Event - ์• ์ดˆ์— ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ํ˜•ํƒœ๋กœ ๋ฉ”์„œ๋“œ ์ •์˜ํ•˜๊ธฐ.

Property Initializer Syntax ๋ผ๋Š”๋ฐ ์ด๋Ÿฐ ์–ด๋ ค์šด ์šฉ์–ด ๋Š” ์ ‘์–ด๋‘๊ณ  ์• ์ดˆ์— ํ•จ์ˆ˜๋ฅผ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด ์ •์˜ํ•ด๋ฒ„๋ฆฌ๋ฉด,

๋ฉ”์„œ๋“œ๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค๋•Œ๋งˆ๋‹ค constructor ์— bind(this) ์ ์–ด์ฃผ๊ณ  ์ˆ˜์ •ํ•  ํ•„์š”๊ฐ€ ์—†์–ด์ง„๋‹ค.

handleChange = event => {
  this.setState({
    message: event.target.value,
  })
}

handleClick = () => {
  alert(this.state.message)
  document.querySelector('#msg').value = ''
  this.setState({
    message: '',
  })
}

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

GitHubMediumTwitterFacebook