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.
npm install react-google-autocompleteVerified import paths — ran on the pinned version, not inferred.
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.
For new projects or extensive customization, prefer `usePlacesWidget` or `usePlacesAutocompleteService`. Refer to the documentation for hook-specific usage and integration patterns.
Verify that both 'Maps JavaScript API' and 'Places API' are enabled in your Google Cloud Console. If manually loading the script, ensure the URL includes `&libraries=places`. If using the `apiKey` prop, ensure the API key is correct and associated with a project where these APIs are active.
Choose one method for loading the Google Maps script: either pass the `apiKey` prop to the `Autocomplete` component/hooks OR manually include the script with `libraries=places` in your `index.html`. Do not do both simultaneously unless specifically handling script loading lifecycle.
Always specify required fields in the `options.fields` prop, for example: `options={{ fields: ["formatted_address", "geometry.location", "address_components"] }}`. Consult the Google Places API documentation for available fields.Go to Google Cloud Console, navigate to 'APIs & Services' -> 'Enabled APIs & services', and ensure 'Maps JavaScript API' is enabled for your project.
If manually loading the script, ensure `&libraries=places` is included in the script URL (e.g., `https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places`). If using the `apiKey` prop, ensure the 'Places API' is enabled in your Google Cloud Console.
Add `geometry` (or specific sub-fields like `geometry.location`) to the `options.fields` prop passed to the component or hook. For example: `options={{ fields: ["formatted_address", "geometry"] }}`.For `Autocomplete`: `import Autocomplete from 'react-google-autocomplete';`. For hooks: `import { usePlacesWidget } from 'react-google-autocomplete';`.