UNPKG

@supunlakmal/hooks

Version:

A collection of reusable React hooks

37 lines 1.7 kB
import { useState, useEffect } from 'react'; import { useGeolocation } from '../browser-web-apis/useGeolocation'; // Assuming path and export import { useFetch } from './useFetch'; // Assuming path and export /** * A hook that fetches data from an API based on the user's current geolocation. * * @param urlBuilder A function that takes latitude and longitude and returns the URL string or Request object. * @param fetchOptions Options for the underlying useFetch hook (standard RequestInit). * @param geolocationOptions Options for the underlying useGeolocation hook. * @returns An object containing geolocation state and fetch state. */ export function useLocationBasedFetch(urlBuilder, // Simplified builder fetchOptions = {}, geolocationOptions = {}) { const geolocationState = useGeolocation(geolocationOptions); const [fetchUrl, setFetchUrl] = useState(undefined); useEffect(() => { if (geolocationState.latitude && geolocationState.longitude) { // Pass lat/lon directly to simplified builder const newUrl = urlBuilder(geolocationState.latitude, geolocationState.longitude); setFetchUrl(newUrl); } else { // Optionally clear fetchUrl or handle cases where location is not available setFetchUrl(undefined); } }, [ geolocationState.latitude, geolocationState.longitude, urlBuilder, // Remove other geolocation state dependencies if only lat/lon are used ]); const fetchState = useFetch(fetchUrl, fetchOptions); return { geolocation: geolocationState, fetch: fetchState, }; } //# sourceMappingURL=useLocationBasedFetch.js.map