A react hook for getting predictions from the Google Places Autocomplete Service

Downloads in past


621.0.42 years ago2 years agoMinified + gzip package size for @atomap/use-places-autocomplete in KB


usePlacesAutocomplete React Hook for using the Google Places Autocomplete Service.
This hook lets you make use of the Google Places Autocomplete Service in the simplest way possible.
You enter text, you get predictions.
This hook uses debouncing which means that you can make sure that if a user is typing it does not spam the service and eat up resources which will save you money when using this service.
I highly recommend that you read this article about using the Google places autocomplete service with react and with hooks


You must have added the Google places script to your html head Add the below script to the <head> of your page and add in your API key.

If you use the Google Places Autocomplete Service you need to know and understand the requirements from Google:
  1. You can use Place Autocomplete even without a map.
  2. If you do show a map, it must be a Google map.
  3. When you display predictions from the Place Autocomplete service without a map, you must include the 'Powered by Google' logo.


Yarn: yarn add @atomap/use-places-autocomplete
NPM: npm install --save @atomap/use-places-autocomplete


import usePlacesAutocomplete from '@atomap/use-places-autocomplete'


Add the following to the head of your html:

Create a new component and paste the following code:
import React, { useState } from 'react'
import usePlacesAutocomplete from '@atomap/use-places-autocomplete'

export default function PredictionsOnInputChange() {
  const [selectedPrediction, setSelectedPrediction] = useState(null)
  const [searchValue, setSearchValue] = useState('')
  const { predictions, error } = usePlacesAutocomplete(searchValue)

  if (error) {

  const handlePredictionSelection = (e, prediction) => {

  return (
          onChange={(e) => setSearchValue(e.target.value)}
          alt="Powered by Google"
          {predictions?.map((prediction) => (
            <li key={prediction?.place_id}>
                onClick={(e) => handlePredictionSelection(e, prediction)}
                onKeyDown={(e) => handlePredictionSelection(e, prediction)}
                {prediction?.structured_formatting?.main_text || 'Not found'}
        <h3>You searched for: {searchValue}</h3>
          You selected:{' '}
          {selectedPrediction?.structured_formatting?.main_text || 'None'}

Example with options:
const { predictions, error } = usePlacesAutocomplete(searchValue, {
  debounceTimeout: 400,
  componentRestrictions: { country: 'gb' },

Types & Arguments

| Argument | TypeScript Type | JavaScript Type | Description | | ---------------------------------- | ---------------------------- | --------------- | -------------------------------------------------- | | userInput | string | string | A search query provided by a user. | | options (optional) | UsePlacesAutocompleteOptions | object | Options for the google places autocomplete service full list available here | | options.debounceTimeout (optional) | number | number | Length of time to wait for the debounce |