๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

JavaScript

[ JS ] e.stopPropagation ๊ณผ e.preventDefault ์ฐจ์ด ์•Œ๊ณ  ๋„˜์–ด๊ฐ€๊ธฐ

 

 

e.stopPropagation()๊ณผ e.preventDefault()๋Š” ๋น„์Šทํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜์ง€๋งŒ, ์•„์˜ˆ ๋‹ค๋ฅธ ์ด๋ฒคํŠธ์ด๋‹ค.

e.stopPropagation()์€ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ์ƒ์œ„ ํƒœ๊ทธ์—์„œ ์‹คํ–‰๋˜์ง€ ์•Š๋„๋ก ๋ง‰๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ๋‹ค์‹œ๋งํ•ด ์ด๋ฒคํŠธ์˜ ๋ฒ„๋ธ”๋ง์„ ์ค‘์ง€ํ•˜์—ฌ ํ˜„์žฌ ์ด๋ฒคํŠธ๊ฐ€ ์ƒ์œ„ ์š”์†Œ(๋ถ€๋ชจ)๋กœ ์ „ํŒŒ๋˜๋Š” ๊ฒƒ์„ ๋ง‰๋Š”๋‹ค.


e.preventDefault() ๋Š” ๊ธฐ๋ณธ ๋ธŒ๋ผ์šฐ์ € ๋™์ž‘์„ ๊ฐ€์ง„ ์ผ๋ถ€ ์ด๋ฒคํŠธ๊ฐ€ ํ•ด๋‹น ๊ธฐ๋ณธ ๋™์ž‘์„ ์‹คํ–‰ํ•˜์ง€ ์•Š๋„๋ก ๋ฐฉ์ง€ํ•œ๋‹ค. ๋ณดํ†ต ๋งํฌ ํด๋ฆญ ์‹œ ํŽ˜์ด์ง€ ์ด๋™๋ง‰๊ธฐ, ํผ ์ œ์ถœ์‹œ ํŽ˜์ด์ง€ ๋ฆฌ๋กœ๋“œ ๋ฐฉ์ง€๋“ฑ์˜ ๊ธฐ๋ณธ ๋™์ž‘ ์ œ์–ด ์—ญํ• ์„ ํ•œ๋‹ค. 

 

 

 


 

 

 

 

e.stopPropagation()

 

์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์˜ ๋ฒ„๋ธ”๋ง์„ ๋ฐฉ์ง€ํ•ด์ค€๋‹ค.

( ๋ฒ„๋ธ”๋ง : ์ด๋ฒคํŠธ์˜ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ถ€๋ชจ๋กœ (์•ˆ์ชฝ -> ๋ฐ”๊นฅ์ชฝ) ์ „ํŒŒ๋˜๋Š” ๊ฒƒ. )

 

export default function Toolbar() {
  return (
    <div className="Toolbar" onClick={() => {
      alert('You clicked on the toolbar!');
    }}>
      <button onClick={() => alert('Playing!')}>
        Play Movie
      </button>
      <button onClick={() => alert('Uploading!')}>
        Upload Image
      </button>
    </div>
  );
}

 

 

์œ„์™€ ๊ฐ™์€ react ์ฝ”๋“œ์—์„œ, `<button>` ์„ ํ•˜๋‚˜๋ผ๋„ ํด๋ฆญํ•˜๋ฉด, ๋ฒ„ํŠผ์˜ alert๊ฐ€ ๋…ธ์ถœ๋˜๊ณ ,

๊ณง์ด์–ด ๋ฐ”๋กœ ๋ถ€๋ชจ์ธ `<div>`์˜ alert๋„ ๋…ธ์ถœ๋˜์–ด ์ด 2๊ฐœ์˜ alert์ฐฝ์ด ๋…ธ์ถœ๋œ๋‹ค.

 

 

 

์ด๋ ‡๊ฒŒ ์ด๋ฒคํŠธ ์ „ํŒŒ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์„ ๋ง‰๊ธฐ์œ„ํ•ด์„œ ์•„๋ž˜์˜ ์ฝ”๋“œ๋กœ ์ˆ˜์ •ํ•˜์—ฌ e.stopPropagation()์„ ํ˜ธ์ถœํ•˜์—ฌ ์ด๋ฒคํŠธ๊ฐ€ ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์— ๋‹ฟ์ง€ ๋ชปํ•˜๋„๋ก ์ „ํŒŒ๋ฅผ ๋ง‰์•„์ฃผ๋ฉด ๋œ๋‹ค. 

 

function Button({ onClick, children }) {
  return (
    <button onClick={e => {
      e.stopPropagation();
      onClick();
    }}>
      {children}
    </button>
  );
}

export default function Toolbar() {
  return (
    <div className="Toolbar" onClick={() => {
      alert('You clicked on the toolbar!');
    }}>
      <Button onClick={() => alert('Playing!')}>
        Play Movie
      </Button>
      <Button onClick={() => alert('Uploading!')}>
        Upload Image
      </Button>
    </div>
  );
}

 

 

 

 

 

 

 

 

 

 

e.preventDefault()

 

์ด๋ฒคํŠธ์˜ ๊ธฐ๋ณธ ๋™์ž‘์„ ๋ฐฉ์ง€ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ, ๋ธŒ๋ผ์šฐ์ € ์ด๋ฒคํŠธ์˜ ๊ธฐ๋ณธ ๋ธŒ๋ผ์šฐ์ € ๋™์ž‘์„ ๋ง‰๋Š” ๊ฒƒ.

์˜ˆ๋ฅผ๋“ค์–ด, `<form>`์˜ submit ์ด๋ฒคํŠธ๋Š” ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ํŽ˜์ด์ง€ ์ „์ฒด๊ฐ€ ๋ฆฌ๋กœ๋“œ ๋˜๋Š” ๊ฒƒ์ด ๊ธฐ๋ณธ ๋™์ž‘์ด๋‹ค.

 

export default function Signup() {
  return (
    <form onSubmit={() => alert('Submitting!')}>
      <input />
      <button>Send</button>
    </form>
  );
}

 

 

submit์˜ ๊ธฐ๋ณธ ๋™์ž‘์œผ๋กœ ํŽ˜์ด์ง€ ์ „์ฒด๊ฐ€ ๊นœ๋นก๊ฑฐ๋ฆฌ๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ•˜๋Š”๋ฐ, ์ด๋Ÿฌํ•œ ๊ธฐ๋ณธ ๋ธŒ๋ผ์šฐ์ € ๋™์ž‘์„ ๋ง‰๊ธฐ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ e.preventDefault()์ด๋‹ค.

 

export default function Signup() {
  return (
    <form onSubmit={e => {
      e.preventDefault();
      alert('Submitting!');
    }}>
      <input />
      <button>Send</button>
    </form>
  );
}

 

์ด๋ ‡๊ฒŒ ๋ธŒ๋ผ์šฐ์ €์˜ ๊ธฐ๋ณธ ๋™์ž‘์„ ๋ง‰์Œ์œผ๋กœ์„œ, submit์„ ํ•˜์—ฌ๋„ ํŽ˜์ด์ง€๊ฐ€ ๊นœ๋นก๊ฑฐ๋ฆฌ๋Š” ํ˜„์ƒ์ด ์‚ฌ๋ผ์ง„๋‹ค.

 

 

 

 

 

 

 

 

 

 

 


 

๐ŸŒธ ์ถœ์ฒ˜ ๐ŸŒธ

 

https://react.dev/learn/responding-to-events#event-propagation

https://ko.react.dev/learn/responding-to-events#can-event-handlers-have-side-effects

https://developer.mozilla.org/ko/docs/Web/API/Event/preventDefault

https://developer.mozilla.org/ko/docs/Web/API/Event/stopPropagation

https://developer.mozilla.org/ko/docs/Web/API/Event/stopImmediatePropagation