Created
April 25, 2025 02:02
-
-
Save hongggyelim/72b23d204a9c17d9420e5a4741cef90a to your computer and use it in GitHub Desktop.
커스텀 훅 useMovePointByWidth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const useMovePointByWidth = () => { | |
const containerRef = useRef<HTMLDivElement | null>(null); // 기준이 될 DOM 요소 | |
const [width, setWidth] = useState(0); | |
const [prevWidth, setPrevWidth] = useState(0); | |
const { points, setPoints } = useGraphStore(); | |
useEffect(() => { | |
if (!containerRef.current) return; | |
const updateWidth = () => { | |
if (containerRef.current) { | |
setWidth(containerRef.current.clientWidth); | |
} | |
}; | |
const debouncedUpdateWidth = debounce(updateWidth, 200); | |
const observer = new ResizeObserver(debouncedUpdateWidth); | |
observer.observe(containerRef.current); | |
updateWidth(); | |
return () => observer.disconnect(); | |
}, []); | |
useEffect(() => { | |
// 이미 저장된 points x좌표 조정 | |
if (width !== prevWidth && prevWidth !== 0) { | |
const newPoints: PointData[] = points.map((point) => ({ ...point, x: point.x * (width / prevWidth) })); | |
setPoints(newPoints); | |
} | |
setPrevWidth(width); | |
}, [width, points, prevWidth]); | |
return { width, points, containerRef }; | |
}; | |
export default useMovePointByWidth; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment