Skip to content

Commit

Permalink
chore: set useCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
Pridesd committed Jun 24, 2024
1 parent 4d43a9a commit 9552b57
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/hooks/use-map.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, type RefObject, useEffect } from 'react'
import { useState, type RefObject, useEffect, useCallback } from 'react'

type LocationType = {
latitude: number
Expand All @@ -20,15 +20,18 @@ const useMap = <T>(
) => {
const [map, setMap] = useState<kakao.maps.Map | null>(null)

const setCurrentLocation = ({ latitude, longitude }: LocationType) => {
window.kakao.maps.load(() => {
const options = {
center: new window.kakao.maps.LatLng(latitude, longitude),
level: initialLevel,
}
setMap(new window.kakao.maps.Map(containerRef.current, options))
})
}
const setCurrentLocation = useCallback(
({ latitude, longitude }: LocationType) => {
window.kakao.maps.load(() => {
const options = {
center: new window.kakao.maps.LatLng(latitude, longitude),
level: initialLevel,
}
setMap(new window.kakao.maps.Map(containerRef.current, options))
})
},
[containerRef, initialLevel],
)

/**
* 지도의 위치를 재설정할 때 사용
Expand Down Expand Up @@ -68,7 +71,7 @@ const useMap = <T>(
}

useEffect(() => {
if (containerRef.current) return
if (!containerRef.current) return

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
Expand All @@ -80,9 +83,7 @@ const useMap = <T>(
} else {
setCurrentLocation(INITIAL_LATITUDE_LONGITUDE)
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [containerRef])
}, [containerRef, setCurrentLocation])

return { map, setLocation, addMarker }
}
Expand Down

0 comments on commit 9552b57

Please sign in to comment.