๐ŸŽฏ ๋ฆฌ์•กํŠธ์—์„œ ์ด๋ฒคํŠธ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•

mangorovskiยท2022๋…„ 10์›” 4์ผ
0

์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋ฐ”์ธ๋”ฉ์ด๋ž€?
์ปดํฌ๋„ŒํŠธ(Component)์™€ ์ด๋ฒคํŠธ ํ•จ์ˆ˜๋ฅผ ์—ฐ๊ฒฐ(bind)ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
๋ฐ”์ธ๋”ฉํ•˜์ง€ ์•Š์•„๋„ ์ด๋ฒคํŠธ ํ•จ์ˆ˜๋Š” ์‹คํ–‰์ด ๋˜์ง€๋งŒ, ๋ˆ„๊ฐ€(์–ด๋–ค ์ปดํฌ๋„ŒํŠธ๊ฐ€) ํ˜ธ์ถœ์„ ํ–ˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
๋ฐ”์ธ๋”ฉํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ, ์ด๋ฒคํŠธ ํ•จ์ˆ˜์—์„œ this.state ๋˜๋Š” this.props๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ undefined๋กœ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค.

1. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ•จ์ˆ˜๋Š” ๊ฐ์ฒด์ด๋‹ค.

 const App = () => {
    const nav = useNavigate()
    const goTo = () => {
      nav('ex-router')
    }
    const goToWrapper = () => {
      goTo()
    }

    return (
        <div>
          <nav>
            <button onClick={goToWrapper}>๊ฐ€์ž!!</button>
          </nav>
        </div>
    );
};
  • ์ด๋ฒคํŠธ ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ํ•จ์ˆ˜์˜ ํ˜•ํƒœ๋กœ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.
 const App = () => {
    const nav = useNavigate()
    const goTo = () => {
      nav('ex-router')
    }

    return (
        <div>
          <nav>
            <button onClick={() => goTo()}>๊ฐ€์ž2!!</button>
          </nav>
        </div>
    );
};
  • ์ด๋ฒคํŠธ ํ• ๋‹น ๋ถ€๋ถ„์— ์ต๋ช…ํ•จ์ˆ˜๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด ํ•จ์ˆ˜์•ˆ์— ํ•จ์ˆ˜์ด๊ธฐ์— ๋ฐ”๋กœ ์‹คํ–‰์ด๋œ๋‹ค.

1.5 ๋ฆฌ์•กํŠธ์—์„œ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋ง ๋ฐฉ๋ฒ•

  1. ์†Œ๋ฌธ์ž ๋Œ€์‹  ์นด๋ฉœ์ผ€์ด์Šค ์‚ฌ์šฉํ•˜๊ธฐ

onClick={changename} (x) onClick={changeName} (o)

<button onClick={activateLasers}>
  Activate Lasers
</button>
  1. JSX๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ํ•จ์ˆ˜๋กœ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์ „๋‹ฌ

{onClickHandle}

  • ์ผ๋ฐ˜์ ์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ๋™์ž‘ ์‹œํ‚ด
  • ํ•จ์ˆ˜ ์ž์ฒด๊ฐ€ ์ „๋‹ฌ๋œ๋‹ค.
  • props๊ฐ€ ์—†์„ ์‹œ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
  • ํ•จ์ˆ˜๋ช… ๋’ค์— ()๋ฅผ ๋ถ™์ด์ง€ ์•Š๋Š”๋‹ค.

{onClickHandle()}

  • ํ•จ์ˆ˜ ํ˜ธ์ถœ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ ์‹คํ–‰ ๋œ๋‹ค.
  • ๋งŒ์ผ onClickํ•จ์ˆ˜์— ํ˜ธ์ถœ๋ฌธ์„ ๋„ฃ๊ฒŒ๋˜๋ฉด ํด๋ฆญ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ๋„ ์ „์— ์‹คํ–‰๋œ๋‹ค.
  • ํ•จ์ˆ˜์—์„œ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’์ด ์—†๋‹ค๋ฉด undefined๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

2. ์ด๋ฒคํŠธ ๊ธฐ๋ณธ ๋™์ž‘ ๋ฐฉ์ง€

e.preventDefault() ํ˜ธ์ถœ

  • ๋ฆฌ์•กํŠธ์—์„œ๋Š” prevetDefault๊ฐ€ ๋ช…์‹œ์ ์œผ๋กœ ํ˜ธ์ถœ๋˜์–ด์•ผํ•œ๋‹ค.
  • ํผ ์ œ์ถœ์‹œ์—๋Š” ๊ณ„์† ๋ Œ๋”๋ง์ด ๋˜๋Š”๋ฐ ๊ณ„์† ๋ Œ๋”๋ง์ด๋˜๋Š” ํ™”๋ฉด์„ ๋ง‰์„ ์ˆ˜ ์žˆ๋‹ค.
  • aํƒœ๊ทธ ์‹œ hrefํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜์ง€ ์•Š๊ณ  ๋„˜๊ฒจ์ค€ ๊ฐ’๋งŒ ์‚ฌ์šฉํ•˜๊ฒŒํ•œ๋‹ค.

ํ•˜๋‚˜์˜ ์›น์‚ฌ์ดํŠธ์— ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ด๋ฒคํŠธ๊ฐ€ ์กด์žฌํ•˜๊ณ  ๊ฐ™์€ ์ด๋ฒคํŠธ๋ผ๊ณ  ํ•˜๋”๋ผ๋„ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•ด์•ผํ•˜๋Š” ๊ฒƒ๋“ค์ด ๋งŽ์•„์ง„๋‹ค.

์ด๋ฒคํŠธ ์ œ์–ด๊ฐ€ ๋ณต์žกํ•ด์ง€๋Š” ๊ทผ๋ณธ์ ์ธ ์›์ธ์€ html ํƒœ๊ทธ๊ฐ€ ์ค‘์ฒฉ๋œ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌด์ธ๋‹ค.

//html
<body>
    <div class="divWrapper">
        <div class="inner1">
            <div class="inner2"></div>
        </div>
    </div>
</body>
// divํƒœ๊ทธ ์•ˆ์— > div > div๊ฐ€ ๊ตฌ์กฐ๋˜์–ด ์žˆ๋‹ค. 
//js
const divs = document.querySelectorAll('div')
divs.forEach(box => {
    box.addEventListner('click', e => {
        console.log(e.currentTarget.className)
    })
})

jsํŒŒ์ผ์—์„œ div๋ฅผ ๊ฐ–๊ณ ์žˆ๋Š” ์š”์†Œ๋ฅผ ๋ชจ๋‘ ๊ฐ–๊ณ ์™€์„œ
์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ํƒ€์ผ“์„ ์ฝ˜์†”์— ์ฐ์–ด์ฃผ๋Š” ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œ์ผฐ๋‹ค.

๊ฒฐ๊ณผ์ ์œผ๋กœ inner2๋ฅผ ๋ˆŒ๋ €์ง€๋งŒ innner1๊ณผ divWrapper๋„ ํ•จ๊ป˜ ์ฝ˜์†”์— ์ฐํžˆ๊ฒŒ ๋œ๋‹ค.
inner2๋ฅผ ํด๋ฆญํ–ˆ์„ ๋•Œ ํ•ด๋‹น ๋ธ”๋ก์„ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ์‹คํ–‰์ปจํ…์ŠคํŠธ์— ์ด๋ฒคํŠธ ๋ฒ„๋ธ”๋ง ํ˜„์ƒ์ด ์ผ์–ด๋‚˜๊ฒŒ ๋œ๋‹ค.

์ด๋ฒคํŠธ ๋ฒ„๋ธ”๋ง?

  • ๋ธŒ๋ผ์šฐ์ €๋Š” inner2์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚ค๊ณ , ์ƒ์œ„์— ์žˆ๋Š” ์š”์†Œ์— ๋™์ผํ•œ ์ด๋ฒคํŠธ๊ฐ€ ๋“ฑ๋ก๋˜์–ด ์žˆ๋Š”์ง€ ํƒ์ƒ‰ํ•œ๋‹ค. ์ด ์ค‘ ๋™์ผํ•œ ์ด๋ฒคํŠธ๊ฐ€ ๋“ฑ๋ก๋˜์–ด ์žˆ๋Š” ์š”์†Œ๊ฐ€ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚จ๋‹ค.

์ด๋ฒคํŠธ ๋ฒ„๋ธ”๋ง์€ ํŠน์ • ์š”์†Œ์—์„œ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ, ์ƒ์œ„์— ์žˆ๋Š” ์š”์†Œ์—๋„ ๋™์ผํ•œ ์ด๋ฒคํŠธ๊ฐ€ ์ ์šฉ๋˜์–ด ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ์š”์†Œ๊นŒ์ง€ ์ด๋ฒคํŠธ๊ฐ€ ์ „ํŒŒ๋˜๋Š”๊ฒƒ์„ ๋งํ•œ๋‹ค.

Event.stopPropagation()

  • ์‚ฌ์‹ค์ƒ ์—ฌ๋Ÿฌ๊ฐœ ๋™์ผํ•œ ์š”์†Œ์— ๋“ฑ๋ก๋œ ์ด๋ฒคํŠธ์˜ ์ „ํŒŒ๊ฐ€ ์–ด๋–ป๊ฒŒ ํ˜๋Ÿฌ๊ฐ€๋Š”์ง€ ํŒŒ์•…ํ•˜๊ธฐ ํž˜๋“ค๋‹ค. Event.stopPropagation()์€ ํŠน์ • ์š”์†Œ์—๋งŒ ์‹คํ–‰ํ•˜๊ณ  ์ „ํŒŒ๋ฅผ ๋ง‰๊ธฐ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.
//js
const divs = document.querySelectorAll('div')
divs.forEach(box => {
    box.addEventListner('click', e => {
    	e.stopPropagation()
        console.log(e.currentTarget.className)
    })
})
//react
import React, { useState } from 'react'

function Test() {
    const [visible, setIsvisible] = useState(false)
    const closeBanner = (e) => { 
        e.stopPropagation()
        setIsvisible(true) 
    }
    return (
        <div
            onClick={() => alert('๋‹น์ฒจ!')}
            style={{ display: visible ? 'flex' : 'none' }}
        >
            <button type="button"
                onClick={closeBanner}
            >๋‹ซ๊ธฐ</button>
        </div>
    )
}

export default Test
  • setIsvisible(true)๋Š” ๋น„๋™๊ธฐ๋กœ ์ฒ˜๋ฆฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— stopPropagation์ด ์—†๋‹ค๋ฉด ํ•ด๋‹น ์ฝ”๋“œ๋Š” ๋‹ค์‹œ ์ƒ์œ„ ์ฝ”๋“œ๊ฐ€ ๋จผ์ € ํ˜ธ์ถœ๋œ๋‹ค.
  • closeBanner ํ•จ์ˆ˜์—์„œ ์ด๋ฒคํŠธ ๊ฐ์ฒด๋ฅผ ๋„˜๊ฒจ์ฃผ๊ณ  ํ•ด๋‹น ๊ฐ์ฒด์— stopPropagation์„ ์ ์šฉ์‹œ์ผœ์ค€๋‹ค.
  • ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์—์„œ ํ•จ์ˆ˜๋ฅผ ์งค๋•Œ ํ•˜๋‚˜์˜ ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ์ค‘์ฒฉ๋œ ์ด๋ฒคํŠธ๋ฅผ ์ž˜ ์‚ดํŽด์•ผํ•œ๋‹ค.
  • ๋˜ํ•œ ์ƒ, ํ•˜์œ„์ปดํฌ๋„ŒํŠธ์—์„œ๋„ ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š”์ง€ ์ž˜ ํ™•์ธํ•ด์•ผํ•œ๋‹ค.

3. ์ด๋ฒคํŠธ์—์„œ์˜ this

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์™€ Function.prototype.bind

  • ์ด๋ฒคํŠธ์—์„œ this๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฝ์šฐ์— ๋ฐ”์ธ๋”ฉ์ด ๋˜์–ด ์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— undefined๊ฐ€ ๋‚˜์˜จ๋‹ค.
  • props ์ „๋‹ฌ ์‹œ์—๋Š” arrowํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • ํ˜น์€ ํ•จ์ˆ˜๋ช… ๋’ค์— .bindํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

4. ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์— ์ธ์ž ์ „๋‹ฌํ•˜๊ธฐ

์ฝœ๋ฐฑํ•จ์ˆ˜ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์— ์ธ์ž๋ฅผ ๋„˜๊ธฐ๋Š” ์ฝ”๋“œ๋Š” ์ƒํ™ฉ๋งˆ๋‹ค ๊ฐ๊ฐ ๋‹ค๋ฅด๋‹ค.

1). [์ฝœ๋ฐฑํ•จ์ˆ˜์— ์–ด๋– ํ•œ ์ธ์ž๋„ ๋„˜๊ธฐ์ง€ ์•Š์„ ๋•Œ]

ํ•จ์ˆ˜ ํ‘œํ˜„์‹์œผ๋กœ ๋”ฐ๋กœ๋นผ์„œ jsx๋ฌธ๋ฒ•์œผ๋กœ ์ค‘๊ด„ํ˜ธ ์•ˆ์— ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์–ด์ฃผ๋Š” ๋ฐฉ์‹์„ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค.

function Test() {
    const onClickHandle = () => {
        alert('hello world')
    }
    return (
        <div>
            <button
                type="button"
                onClick={onClickHandle}
            >Click Me!</button>
        </div>
    )
}

export default Test

๊ฐ„๋‹จํ•œ ๋ฌธ๊ตฌ๋ผ๋ฉด ํ•จ์ˆ˜ ์‹คํ–‰์„ inline์œผ๋กœ ๋„ฃ์–ด์ค˜๋„ ๊ดœ์ฐฎ!
//onClick={() => alert('hello world')}

2). [(์ด๋ฒคํŠธ ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ) ์ผ๋ฐ˜์ ์ธ ์ธ์ž๋ฅผ ๋„˜๊ธธ ๋•Œ]

// name์ด๋ผ๋Š” ๋ฌธ์ž์—ด ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›๋Š”๋‹ค.
import React, { useState } from 'react'

function Test() {
    const onClickHandle = (name) => {
        alert(`hello, ${name}`)
    }
    return (
        <div>
            <button
                type="button"
                onClick={onClickHandle('nimo')}
            >Click Me!</button>
        </div>
    )
}

export default Test

์—ฌ๊ธฐ์„œ ๋ฌธ์ œ์ ์€ onClick={onClickHandle('nimo')}๋ถ€๋ถ„์—์„œ ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ๋ฐ”๋กœํ•ด์คฌ๊ธฐ ๋•Œ๋ฌธ์— onclick ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒ๋˜๊ธฐ๋„ ์ „์— alert์ด ๋จผ์ € ์‹คํ–‰๋˜๋ฒ„๋ฆฐ๋‹ค.

์ฝœ๋ฐฑํ•จ์ˆ˜์—์„œ ์ธ์ž๊ฐ’์„ ๋„˜๊ฒจ ์ค„ ๋•Œ๋Š” ํ•จ์ˆ˜ํ˜ธ์ถœ์ด ์•„๋‹Œ ํ•จ์ˆ˜ ์„ ์–ธ์‹์„ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

import React, { useState } from 'react'

function Test() {
    const onClickHandle = (name) => {
        alert(`hello, ${name}`)
    }
    return (
        <div>
            <button
                type="button"
                onClick={() => onClickHandle('nimo')}
            >Click Me!</button>
        </div>
    )
}

export default Test

3). [์ด๋ฒคํŠธ ๊ฐ์ฒด๋งŒ์„ ์ธ์ž๋กœ ๋„˜๊ธธ ๋•Œ]

์ด๋ฒคํŠธ๊ฐ์ฒด๋ž€?
์ด๋ฒคํŠธ๋ฅผ ๋งŒ๋“ค์—ˆ์„ ๋•Œ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด์ง€๋Š” ๊ฐ์ฒด๋ฅผ ๋งํ•œ๋‹ค.
์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๊ฐ์ฒด์ด๋‹ค.

import React, { useState } from 'react'

function Test() {
    const onClickHandle = (e) => {//ํด๋ฆญํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜ ๋˜ํ•œ ์ธ์ž๋ฅผ ๋ฐ›๋Š” ๋ถ€๋ถ„์„ ๋ณ€๊ฒฝ
        alert(e.target.value)
    }
    return (
        <div>
            <button
                type="button"
                value={'clickBtn'}
                onClick={(e) => onClickHandle(e)}
            >Click Me!</button>
        </div>
    )
}

export default Test

๋งŒ์ผ ์ด๋ฒคํŠธ ๊ฐ์ฒด ํ•˜๋‚˜๋งŒ์„ ๋„˜๊ธฐ๋ ค๊ณ  ํ• ๋•Œ๋Š” ์ค„์—ฌ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
`
onClick = {(e) => onClickHandle(e)}

โ–ผ

onClick = { onClickHandle }`

4). [์ด๋ฒคํŠธ ๊ฐ์ฒด์™€ ์ด๋ฒคํŠธ ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ์ธ์ž๋ฅผ ํ•จ๊ป˜ ๋„˜๊ธธ ๋•Œ]

import React, { useState } from 'react'

function Test() {
    const onClickHandle = (e, name) => {
        alert(e.target.value)
        console.log(name)
    }
    return (
        <div>
            <button
                type="button"
                value={'clickBtn'}
                onClick={(e) => onClickHandle(e, 'nimo')}
            >Click Me!</button>
        </div>
    )
}

export default Test

์ด๋ฒคํŠธ ๊ฐ์ฒด์™€ ์ผ๋ฐ˜ ์ธ์ž๋ฅผ ๋„˜๊ธธ ๋•Œ ํ•จ์ˆ˜ ์ปค๋ง์—๋Œ€ํ•ด์„œ๋„ ์ž ๊น ์‚ดํŽด๋ณด์ž.

[ํ•จ์ˆ˜ ์ปค๋ง]:
ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ธ์ž๋ฅผ ํ•œ๋ฒˆ์— ๋„˜๊ธฐ์ง€ ์•Š๊ณ , ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ์ธ์ž๋ฅผ ๋„˜๊ธฐ๊ณ , ํ•จ์ˆ˜์˜ ์ธ์ž๋ฅผ ๊ฐœ์ˆ˜๋งŒํผ ์—ฌ๋Ÿฌ๋ฒˆ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹์„ ๋งํ•œ๋‹ค.
์•Œ์•„์•ผ ํ•  ๋ถ€๋ถ„: ์ผ๊ธ‰ ๊ฐ์ฒด, ํด๋กœ์ €, ๋ ‰์‹œ์ปฌ ์Šค์ฝ”ํ”„, ์Šค์ฝ”ํ”„ ์ฒด์ธ


import React, { useState } from 'react'

function Test() {
    const onClickHandle = name => e => {
        alert(e.target.value)
        console.log(name)
    }
    return (
        <div>
            <button
                type="button"
                value={'clickBtn'}
                onClick={(e) => onClickHandle('nimo')(e)}
            // onClick={onClickHandle('nimo')} ์ด๋ ‡๊ฒŒ ์ค„์—ฌ๋„ ๋จ
            >Click Me!</button>
        </div>
    )
}

export default Test
profile
๋น„๋‹ˆ๋กœ๊ทธ ์ณŒํ‚จ

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