Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚩 react 메인 νŽ˜μ΄μ§€μ—μ„œ 툴팁 말풍선 λ„μš°λ©΄μ„œ 배운점 정리 #15

Open
leemember opened this issue Jul 4, 2022 · 0 comments

Comments

@leemember
Copy link
Owner

leemember commented Jul 4, 2022

react 메인 νŽ˜μ΄μ§€μ—μ„œ SIMPLE μ„œλΉ„μŠ€ 툴팁 말풍선 λ„μš°λ©΄μ„œ 배운점 정리

// 슀크둀 μ΄λ²€νŠΈμ™€ 클릭 이벀트 μ‹€ν–‰μ‹œ 말풍선 사라지도둝
useEffect(() => {
   window.addEventListener('scroll', handleScroll);
   window.addEventListener('click', handleScroll);
   return () => {
      window.removeEventListener('scroll', handleScroll);
      window.removeEventListener('click', handleScroll);
   };
}, []);
 
// 슀크둀, 클릭 이벀트
const handleScroll = (e) => {
   // 슀크둀 μ‹œ, 아무곳 ν΄λ¦­μ‹œ simpleTip 말풍선 μ‚¬λΌμ§€κ²Œ
   if (window.scrollY >= 100 || onModal && (!el.current || !el.current?.contains(e.target))){
      setOnModal(false)
   }
};

useEffectλ₯Ό μ‚¬μš©ν•΄μ„œ 슀크둀 μ΄λ²€νŠΈλž‘ click μ΄λ²€νŠΈκ°€ 일어날 λ•Œ handleScroll μ΄λΌλŠ” ν•¨μˆ˜κ°€ λ™μž‘λ˜λ„λ‘ ν–ˆλ‹€.
κ·Έλž˜μ„œ useEffect κΈ°λŠ₯내에 μžˆλŠ” retrun 값을 톡해 cleanup μž‘μ—…κΉŒμ§€ ν–ˆλ‹€. (removeEventListener == 이벀트λ₯Ό μ§€μ›Œμ£ΌλŠ”)

그리고 μ„œλΉ„μŠ€ 툴팁 말풍선이 μ‚¬λΌμ§€λŠ” 쑰건은 2가지닀

  1. 슀크둀 μ‹œ
  2. 아무 κ³³ 클릭할 λ•Œ

근데 μž‘μ—…ν•˜λ©΄μ„œ 문제점이 무엇이냐면,
λ©”μΈνŽ˜μ΄μ§€μ— 첫 번째둜 λ‚˜νƒ€λ‚˜λŠ” λͺ¨λ‹¬μ„ ν΄λ¦­ν•˜μ—¬ λ‹«κ²Œ 될 경우, 이벀트 버블링이 μΌμ–΄λ‚˜μ„œ λ§ν’μ„ κΉŒμ§€ λ™μ‹œμ— 사라진닀.
κ·Έλž˜μ„œ 첫 번째둜 λ‚˜νƒ€λ‚˜λŠ” λͺ¨λ‹¬μ„ 닫을 λ•Œμ— close λ˜λŠ” 이벀트 μͺ½μ—λ‹€κ°€ 이벀트 버블링을 μ•„λž˜ λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•΄μ„œ λ§‰μ•„μ£Όμ—ˆλ‹€.

event.stopPropagation()
을 μ‚¬μš©ν•΄μ£Όλ©΄ λœλ‹€.

πŸ₯ 이벀트 λ²„λΈ”λ§μ΄λž€ ?

μ΄λ²€νŠΈκ°€ μ—°μ†ν•˜μ—¬ λ°œμƒν•˜λŠ” 버블 ν˜„μƒμ„ μ˜λ―Έν•œλ‹€.
μžμ‹ μ—˜λ¦¬λ¨ΌνŠΈμ—μ„œ 이벀트 λ°œμƒ μ‹œ, λΆ€λͺ¨ μ—˜λ¦¬λ¨ΌνŠΈμ— 같은 μ΄λ²€νŠΈκ°€ μ„€μ •λ˜μ–΄ μžˆλ‹€λ©΄ ν•΄λ‹Ή μ΄λ²€νŠΈμ— ν• λ‹Ήλœ κΈ°λŠ₯도 ν•¨κ»˜ μ‹€ν–‰λœλ‹€.
κ·Έλž˜μ„œ 이 같은 ν˜„μƒμ„ 막기 μœ„ν•΄μ„œλŠ” e.stopPropagation() 을 μ‚¬μš©ν•΄μ„œ 막아주면 λœλ‹€.

@leemember leemember changed the title 🚩 react 메인 νŽ˜μ΄μ§€μ—μ„œ SIMPLE μ„œλΉ„μŠ€ 툴팁 말풍선 λ„μš°λ©΄μ„œ 배운점 정리 🚩 react 메인 νŽ˜μ΄μ§€μ—μ„œ 툴팁 말풍선 λ„μš°λ©΄μ„œ 배운점 정리 Feb 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant