diff --git a/index.d.ts b/index.d.ts index 9ef3f1e..75eb0a3 100644 --- a/index.d.ts +++ b/index.d.ts @@ -29,5 +29,10 @@ declare module 'react-native-geocoder-reborn' { geocodeAddress: (address: string) => Promise, }; + export const useGeocodePosition: (position: Position | null) => { + geocodePosition: GeocodingObject[] | null + error: any | null + } + export default geocoder; } diff --git a/index.js b/index.js index 2a59203..1c3c314 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ import Geocoder from './src/geocoder.js'; +export { useGeocodePosition } from './src/hooks' export default Geocoder; diff --git a/src/hooks.js b/src/hooks.js new file mode 100644 index 0000000..b1a3832 --- /dev/null +++ b/src/hooks.js @@ -0,0 +1,22 @@ +import { useEffect, useState } from 'react' +import Geocoder from './geocoder' + +export const useGeocodePosition = (position) => { + const [geocodePosition, setGeocodePosition] = useState(null) + const [error, setError] = useState(null) + + useEffect(() => { + (async () => { + if (!position) return + + try { + const geocodePositions = await Geocoder.geocodePosition(position) + setGeocodePosition(geocodePositions) + } catch (error) { + setError(error) + } + })() + }, [position?.lat, position?.lng]) + + return { geocodePosition, error } +}