1.0.1 • Published 4 years ago

use-google-places v1.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

React Hook useGooglePlaces

Hook to consume Google Places API returning both autocomplete with debounce and geocoding callback method

Flow

onChange<keyword> -> Fetch autocomplete -> results -> onSelect -> Fetch geocoding -> onSelect.callback<result>

Installation

  • TODO

Options

PropDescriptionDefault
apiKeyClient-side google api key (documentation)https://developers.google.com/places/web-service/get-api-key.None
delaydebounce OnChange (milliseconds).0

Methods

PropDescriptionType
loadingTriggers each request.boolean
resultsNormalized Google API (predictions)https://developers.google.com/places/web-service/autocomplete#place_autocomplete_responses.{title, subtitle, placeId, description}
onChangeFunction that triggers autocomplete.function<string>
onSelectOptional function that triggers geocoding executing a callback with coordinates and details.function<{placeId}, callback<geocoding-payload>

Usage

Component

  const { loading, results, onChange, onSelect } = useGooglePlaces({
    apiKey: 'my-google-api-key',
    delay: 400,
  });

  ...

  const onPress = item => {
    onSelect(item, ({ coordinates }) => {
      dispatch({
        type: SET_LOCATION,
        payload: coordinates,
      });
    });
  };

  ...

  <FlatList
    data={results}
    renderItem={({ item }) => (
      <LocationItem
        title={item.title}
        subtitle={item.subtitle}
        onPress={onPress}
      />
    )}
  />