Skip to content

Commit 7b3fd18

Browse files
committed
♻️ element ref를 위한 이펙트 로직들을 useRefEffect로 추출
1 parent 1a07d7b commit 7b3fd18

File tree

2 files changed

+23
-29
lines changed

2 files changed

+23
-29
lines changed

src/components/Slack/SlackMessageItem/components/SlackMessageAttachment/SlackMessageYoutubeAttachment.tsx

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use client'
22

3-
import { useEffect, useRef, useState } from 'react'
3+
import { useState } from 'react'
44
import { FaYoutube } from 'react-icons/fa'
55

66
import AutoHeightImage from '@/components/AutoHeightImage'
77
import { SlackMessageAttachmentLayout } from '@/components/Slack/SlackMessageItem/components/SlackMessageAttachment/SlackMessageAttachmentLayout'
88
import { MessageYoutubeAttachmentItem } from '@/types/schema'
9+
import { useRefEffect } from '@toss/react'
910

1011
interface SlackMessageYoutubeAttachmentProps {
1112
attachment: MessageYoutubeAttachmentItem
@@ -14,29 +15,26 @@ interface SlackMessageYoutubeAttachmentProps {
1415
export const SlackMessageYoutubeAttachment = ({
1516
attachment,
1617
}: SlackMessageYoutubeAttachmentProps) => {
17-
const containerRef = useRef<HTMLDivElement>(null)
1818
const [playing, setPlaying] = useState(false)
1919

20-
useEffect(() => {
21-
const container = containerRef.current
22-
if (!container) {
23-
return
24-
}
20+
const containerRef = useRefEffect<HTMLDivElement>(
21+
(container) => {
22+
const handleResize = () => {
23+
const aspectRatio = attachment.thumbWidth / attachment.thumbHeight
24+
const { width } = container.getBoundingClientRect()
25+
const height = Math.floor(width / aspectRatio)
26+
container.style.height = `${height}px`
27+
}
2528

26-
const handleResize = () => {
27-
const aspectRatio = attachment.thumbWidth / attachment.thumbHeight
28-
const { width } = container.getBoundingClientRect()
29-
const height = Math.floor(width / aspectRatio)
30-
container.style.height = `${height}px`
31-
}
29+
window.addEventListener('resize', handleResize)
30+
handleResize()
3231

33-
window.addEventListener('resize', handleResize)
34-
handleResize()
35-
36-
return () => {
37-
window.removeEventListener('resize', handleResize)
38-
}
39-
}, [attachment.thumbWidth, attachment.thumbHeight])
32+
return () => {
33+
window.removeEventListener('resize', handleResize)
34+
}
35+
},
36+
[attachment.thumbWidth, attachment.thumbHeight]
37+
)
4038

4139
return (
4240
<SlackMessageAttachmentLayout>

src/hooks/useHover.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use client'
22

3-
import { useEffect, useRef, useState } from 'react'
3+
import { useState } from 'react'
4+
5+
import { useRefEffect } from '@toss/react'
46

57
export const useHover = <T extends HTMLElement = HTMLElement>() => {
6-
const ref = useRef<null | T>(null)
78
const [value, setValue] = useState<boolean>(false)
89

910
const handleMouseEnter = () => {
@@ -13,20 +14,15 @@ export const useHover = <T extends HTMLElement = HTMLElement>() => {
1314
setValue(false)
1415
}
1516

16-
useEffect(() => {
17-
const target = ref.current
18-
if (!target) {
19-
return
20-
}
21-
17+
const ref = useRefEffect<T>((target) => {
2218
target.addEventListener('mouseenter', handleMouseEnter)
2319
target.addEventListener('mouseleave', handleMouseLeave)
2420

2521
return () => {
2622
target.removeEventListener('mouseenter', handleMouseEnter)
2723
target.removeEventListener('mouseleave', handleMouseLeave)
2824
}
29-
}, [ref])
25+
}, [])
3026

3127
return [ref, value] as const
3228
}

0 commit comments

Comments
 (0)