React Google Autocomplete & Places Hooks

2.7.5 · active · verified Sun Apr 19

The `react-google-autocomplete` package provides flexible tools for integrating Google Places Autocomplete functionality into React applications. It offers a high-level `Autocomplete` component for quick setup, and lower-level hooks (`usePlacesWidget`, `usePlacesAutocompleteService`) for more control over UI and API interactions. The `usePlacesWidget` hook allows attaching autocomplete behavior to any input element via a ref, while `usePlacesAutocompleteService` provides direct programmatic access to the Google Places Autocomplete Service, including debounce functionality to optimize API requests. The current stable version is 2.7.5, with an active release cadence involving frequent minor and patch updates. Key differentiators include the dual approach of providing both a ready-to-use component and advanced hooks, built-in debounce, and comprehensive TypeScript types.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `Autocomplete` component with a Google Maps API key to integrate Google Places Autocomplete, logging selected place details to the console.

import Autocomplete from "react-google-autocomplete";
import React from 'react';

function MyAutocompleteComponent() {
  // Replace with your actual Google Maps API key or use environment variables
  // Ensure GOOGLE_MAPS_API_KEY is defined in your environment or .env file
  const YOUR_GOOGLE_MAPS_API_KEY = process.env.GOOGLE_MAPS_API_KEY ?? '';

  if (!YOUR_GOOGLE_MAPS_API_KEY) {
    console.error('GOOGLE_MAPS_API_KEY is not defined. Please provide your API key.');
    return <div>API Key missing. Please set GOOGLE_MAPS_API_KEY.</div>;
  }

  return (
    <div style={{ padding: '20px', maxWidth: '600px', margin: 'auto' }}>
      <h1>Search for a Place</h1>
      <p>Start typing an address or place name below:</p>
      <Autocomplete
        apiKey={YOUR_GOOGLE_MAPS_API_KEY}
        onPlaceSelected={(place, inputRef, autocompleteRef) => {
          console.log("Selected Place:", place);
          if (place.geometry && place.geometry.location) {
            console.log("Latitude:", place.geometry.location.lat());
            console.log("Longitude:", place.geometry.location.lng());
          }
          console.log("Full Address:", place.formatted_address);
        }}
        options={{
          types: ["(cities)"], // Example: restrict to cities
          fields: ["address_components", "geometry", "icon", "name", "place_id", "formatted_address"]
        }}
        placeholder="Enter a city or location"
        style={{ 
          width: '100%', 
          padding: '10px',
          fontSize: '16px',
          border: '1px solid #ccc',
          borderRadius: '4px'
        }}
        className="my-autocomplete-input"
      />
      <p style={{ marginTop: '20px', fontSize: '0.9em', color: '#666' }}>
        Ensure the Google Maps JavaScript API and Places API are enabled for your key.
      </p>
    </div>
  );
}

export default MyAutocompleteComponent;

view raw JSON →