Hooks

useGeolocate

Runs geolocation to get the user's location and returns a Homeflow place object containing a label and a place ID.

import React from 'react';
import { useSelector } from 'react-redux';
import { useGeolocate } from 'homeflowjs/hooks';
import { propertySearch } from 'homeflowjs/search';
const MyComponent = () => {
const { handleGeolocate, location } = useGeolocate();
const currentSearch = useSelector((state) => state.search.currentSearch);
if (location) {
const { label, place } = location;
const newSearch = {
...currentSearch,
q: label,
placeId: place,
isQuerySearch: false,
};
// this uses the result of geolocation to instantly run a new search
// although you could also just populate the search form etc
propertySearch(newSearch);
}
return (
<button
type="button"
onClick={handleGeolocate}
>
Geolocate
</button>
);
};