Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
U

use-places-autocomplete

> 前端框架
Open source

React hook for Google Maps Places Autocomplete.

1.3K stars0 likes2 views
WebsiteGitHub

About

React hook for Google Maps Places Autocomplete.

We're actively seeking dedicated maintainers for the repo — if you're interested, please contact me directly.

USE-PLACES-AUTOCOMPLETE

This is a React hook for Google Maps Places Autocomplete, which helps you build a UI component with the feature of place autocomplete easily! By leveraging the power of Google Maps Places API, you can provide a great UX (user experience) for user interacts with your search bar or form, etc. Hope you guys it.

❤️ it? ⭐️ it on GitHub or Tweet about it.

Live Demo

⚡️ Try yourself: https://use-places-autocomplete.netlify.app

Features

  • Provides intelligent places suggestions powered by Google Maps Places API.
  • Builds your own customized autocomplete UI by React hook.
  • Utility functions to do geocoding and get geographic coordinates using Google Maps Geocoding API.
  • Built-in cache mechanism for you to save the cost of Google APIs.
  • Built-in debounce mechanism for you to lower the cost of Google APIs.
  • Supports asynchronous Google script loading.
  • Supports TypeScript type definition.
  • ⌨️ Builds a UX-rich component (e.g. WAI-ARIA compliant and keyword support) via comprehensive demo code.
  • Tiny size (~ 1.7KB gzipped). No external dependencies, aside from the react.

Requirement

To use use-places-autocomplete, you must use [email protected] or greater which includes hooks.

Installation

This package is distributed via npm.

bash
$ yarn add use-places-autocomplete
# or
$ npm install --save use-places-autocomplete

When working with TypeScript you need to install the @types/google.maps as a devDependencies.

bash
$ yarn add --dev @types/google.maps
# or
$ npm install --save-dev @types/google.maps

Getting Started

usePlacesAutocomplete is based on the Places Autocomplete (or more specific docs) of Google Maps Place API. If you are unfamiliar with these APIs, we recommend you review them before we start.

Setup APIs

To use this hook, there're two things we need to do:

  1. Enable Google Maps Places API.
  2. Get an API key.

Load the library

Use the script tag to load the library in your project and pass the value of the callback parameter to the callbackName option.

javascript

⚠️ If you got a global function not found error. Make sure usePlaceAutocomplete is declared before the script was loaded. You can use the async or defer attribute of the `` element to achieve that.

Create the component

Now we can start to build our component. Check the API out to learn more.

…

react-cool-onclickoutside is my other hook library, which can help you handle the interaction of user clicks outside of the component(s).

Easy right? This is the magic of usePlacesAutocomplete ✨. I just showed you how it works via a minimal example. However, you can build a UX rich autocomplete component, like WAI-ARIA compliant and keyword interaction like my demo, by checking the code or integrating this hook with the combobox of Reach UI to achieve that.

…

Lazily Initializing The Hook

When loading the Google Maps Places API via a 3rd-party library, you may need to wait for the script to be ready before using this hook. However, you can lazily initialize the hook in the following ways, depending on your use case.

Option 1, manually initialize the hook:

javascript
import usePlacesAutocomplete from "use-places-autocomplete";

const App = () => {
  const { init } = usePlacesAutocomplete({
    initOnMount: false, // Disable initializing when the component mounts, default is true
  });

  const [loading] = useGoogleMapsApi({
    library: "places",
    onLoad: () => init(), // Lazily initializing the hook when the script is ready
  });

  return 
;
};

Option 2, wrap the hook into a conditional component:

javascript
import usePlacesAutocomplete from "use-places-autocomplete";

const PlacesAutocomplete = () => {
  const { ready, value, suggestions, setValue } = usePlacesAutocomplete();

  return 
;
};

const App = () => {
  const [loading] = useGoogleMapsApi({ library: "places" });

  return (
    

  );
};

Cache Data For You

By default, this library caches the response data to help you save the cost of Google Maps Places API and optimize search performance.

javascript
const methods = usePlacesAutocomplete({
  // Provide the cache time in seconds, the default is 24 hours
  cache: 24 * 60 * 60,
});

By the way, the cached data is stored via the Window.sessionStorage API.

Custom cache key

You may need to have multiple caches. For example, if you use different place type restrictions for different pickers in your app.

javascript
const methods = usePlacesAutocomplete({
  // Provide a custom cache key
  cacheKey: "region-restricted",
});

Note that usePlacesAutocomplete will prefix this with upa-, so the above would become upa-region-restricted in sessionStorage.

API

javascript
const returnObj = usePlacesAutocomplete(parameterObj);

Parameter object (optional)

When using usePlacesAutocomplete, you can configure the following options via the parameter.

Key Type Default Description
requestOptions object The request options of Google Maps Places API except for input (e.g. bounds, radius, etc.).
googleMaps object window.google.maps In case you want to provide your own Google Maps object, pass the google.maps to it.
callbackName string The value of the callback parameter when loading the Google Maps JavaScript library.
debounce number 200 Number of milliseconds to delay before making a request to Google Maps Places API.
cache number | false 86400 (24 hours) Number of seconds to cache the response data of Google Maps Places API.
cacheKey string "upa" Optional cache key so one can use multiple caches if needed.
defaultValue string "" Default value for the input element.
initOnMount boolean true Initialize the hook with Google Maps Places API when the component mounts.

Return object

It's returned with the following properties.

Key Type Default Description
ready boolean false The ready status of usePlacesAutocomplete.
value string "" value for the input element.
suggestions object { loading: false, status: "", data: [] } See suggestions.
setValue function (value, shouldFetchData = true) => {} See setValue.
clearSuggestions function See clearSuggestions.
clearCache function (key = cacheKey) => {} Clears the cached data.
init function Useful when lazily initializing the hook.

suggestions

The search result of Google Maps Places API, which contains the following properties:

  • loading: boolean - indicates the status of a request is pending or has been completed. It's useful for displaying a loading indicator for the user.
  • status: string - indicates the status of the API response, which has these values. It's useful to decide whether we should display the dropdown or

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptautocompletegeocodinggeocoding-apigoogle-maps

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架