React hook for Google Maps Places Autocomplete.
npm install use-places-autocompleteThis 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.









!demo
β‘οΈ Try yourself: https://use-places-autocomplete.netlify.app
- π§ 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.
To use use-places-autocomplete, you must use react@16.8.0 or greater which includes hooks.
This package is distributed via npm.
``sh`
$ yarn add use-places-autocompleteor
$ npm install --save use-places-autocomplete
When working with TypeScript you need to install the @types/google.maps as a devDependencies.
`sh`
$ yarn add --dev @types/google.mapsor
$ npm install --save-dev @types/google.maps
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.
To use this hook, there're two things we need to do:
1. Enable Google Maps Places API.
2. Get an API key.
Use the script tag to load the library in your project and pass the value of the callback parameter to the callbackName option.
`js`
defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=YOUR_CALLBACK_NAME"
>
> β οΈ 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