Gathering detailed insights and metrics for react-google-autocomplete
Gathering detailed insights and metrics for react-google-autocomplete
Gathering detailed insights and metrics for react-google-autocomplete
Gathering detailed insights and metrics for react-google-autocomplete
React components for google places API.
npm install react-google-autocomplete
Mutation observer check existence
Published on 24 Nov 2021
Add refreshToken method to the service autocomplete
Published on 01 Nov 2021
Add libraries prop
Published on 25 Oct 2021
Fix closing brackets for selector
Published on 26 Aug 2021
Fix TS wrong types for autocomplete service
Published on 23 Aug 2021
Add places services into usePlacesAutocompleteService and expose sessionToken to outside
Published on 02 Jun 2021
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
480 Stars
115 Commits
114 Forks
9 Watching
6 Branches
23 Contributors
Updated on 14 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-7.3%
29,320
Compared to previous day
Last week
-0.9%
154,357
Compared to previous week
Last month
-6.6%
665,429
Compared to previous month
Last year
22.8%
7,244,298
Compared to previous year
2
1
ReactGoogleAutocomplete
does but it does not create any dom elements. Instead, it gives you back a react ref which you can set to any input you want.debounce
prop which will reduce the amount of requests users send to Google.If you find this package helpful please give it a star because it hepls it grow and motivates us to build new features and support the old ones.
npm i react-google-autocomplete --save
or
yarn add react-google-autocomplete
As of version 1.2.4, you can now pass an apiKey
prop to automatically load the Google maps scripts. The api key can be found in your google cloud console.. The places service hook requires both the Places API and Maps Javascript API to be enabled.
1<Autocomplete 2 apiKey={YOUR_GOOGLE_MAPS_API_KEY} 3 onPlaceSelected={(place) => console.log(place)} 4/> 5or 6const { ref } = usePlacesWidget({ 7 apiKey: YOUR_GOOGLE_MAPS_API_KEY, 8 onPlaceSelected: (place) => console.log(place) 9}) 10 11<AnyInput ref={ref} />
Alternatively if not passing the apiKey
prop, you can include google autocomplete link api in your app. Somewhere in index.html or somewhere else. More info here
1<script 2 type="text/javascript" 3 src="https://maps.googleapis.com/maps/api/js?key=[YOUR_API_KEY]&libraries=places" 4></script>
This is a simple react component for working with google autocomplete
1import Autocomplete from "react-google-autocomplete"; 2 3<Autocomplete 4 apiKey={YOUR_GOOGLE_MAPS_API_KEY} 5 onPlaceSelected={(place) => { 6 console.log(place); 7 }} 8/>;
apiKey
: pass to automatically load the Google maps scripts. The api key can be found in your google cloud console.
ref
: React ref to be assigned the underlying text input ref.
onPlaceSelected: (place:
PlaceResult, inputRef,
autocompleteRef) => void
: The function gets invoked every time a user chooses location.
options
: Google autocomplete options.
options.types
: By default it uses (cities).options.fields
: By default it uses address_components
, geometry.location
, place_id
, formatted_address
.inputAutocompleteValue
: Autocomplete value to be set to the underlying input.
googleMapsScriptBaseUrl
: Provide custom google maps url. By default https://maps.googleapis.com/maps/api/js
defaultValue
prop is used for setting up the default value e.g defaultValue={'Amsterdam'}
.
language
: Set language to be used for the results. If not specified, Google defaults to load the most appropriate language based on the users location or browser setting.
libraries
: prop is used for loading additional google libraries alongside the places api, defaultValue={["places"]}
.
You can pass any prop specified for the hmtl input tag. You can also set options.fields prop if you need extra information, now it defaults to basic data in order to control expenses.
Is a hook that has a single config argument. It has exactly the same interface as ReactGoogleAutocomplete props. This hook is actually used in the ReactGoogleAutocomplete component.
1import { usePlacesWidget } from "react-google-autocomplete"; 2 3export default () => { 4 const { ref, autocompleteRef } = usePlacesWidget({ 5 apiKey:YOUR_GOOGLE_MAPS_API_KEY, 6 onPlaceSelected: (place) => { 7 console.log(place); 8 } 9 }); 10 11 return <AnyInput ref={ref} {...anyOtherProp}> 12}
It has only one config argument which has propperties: apiKey
, ref
, onPlaceSelected
, options
, inputAutocompleteValue
, googleMapsScriptBaseUrl
. The same props described here
This hook returns object with only two properties:
ref
- is a react ref which you can assign to any input you would like.autocompleteRef
- is the autocomplete instance
This is an initial implementation of debounced google places autocomplete service. It gives you an option to reduce the amount of requests sent to google which reduce your costs. For the time being we decided to use lodash.debounce
to save time and in the later versions we might write our own implementation of debounce with hooks. Because it uses lodash we also decided to not put it into the index library file so it lives in its own file and could be only imported by it.
1import usePlacesService from "react-google-autocomplete/lib/usePlacesAutocompleteService"; 2 3export default () => { 4 const { 5 placesService, 6 placePredictions, 7 getPlacePredictions, 8 isPlacePredictionsLoading, 9 } = usePlacesService({ 10 apiKey: process.env.REACT_APP_GOOGLE, 11 }); 12 13 useEffect(() => { 14 // fetch place details for the first element in placePredictions array 15 if (placePredictions.length) 16 placesService?.getDetails( 17 { 18 placeId: placePredictions[0].place_id, 19 }, 20 (placeDetails) => savePlaceDetailsToState(placeDetails) 21 ); 22 }, [placePredictions]); 23 24 return ( 25 <> 26 <Input 27 placeholder="Debounce 500 ms" 28 onChange={(evt) => { 29 getPlacePredictions({ input: evt.target.value }); 30 }} 31 loading={isPlacePredictionsLoading} 32 /> 33 {placePredictions.map((item) => renderItem(item))} 34 </> 35 ); 36};
The hook has only one config argument.
config
:
apiKey
: Google api key, otherwise google api has to be loaded manually.googleMapsScriptBaseUrl
: Provide custom google maps url. By default https://maps.googleapis.com/maps/api/js
.debounce
: Number of milliseconds to accumulate responses for.options
: Default options which will be passed to every request.sessionToken
: If true then a session token will be attached to every request.language
: If the language code is set, the results will be returned in the specificed languagelibraries
: prop is used for loading additional google libraries alongside the places api, defaultValue={["places"]}
.The hook returns an object with properties:
placesAutocompleteService
: Instance of AutocompleteServiceplacesService
: Instance of PlacesServiceautocompleteSessionToken
: Instance of AutocompleteSessionToken. You can use this to group several requests into a single sessionrefreshSessionToken
: call this function if you need to refresh the session tokenplacePredictions
: an array of AutocompletePredictionisPlacePredictionsLoading
: sets to true when a getPlacePredictions
request is being sent and not yet resolved.getPlacePredictions: (opt:
Options): void
: a function which you call whenever you want to request places predictions. Takes one argument.queryPredictions
: an array of QueryAutocompletePredictionisQueryPredictionsLoading
: sets to true when getQueryPredictions
request is being sent and not yet resolved.getQueryPredictions: (opt:
Options): void
: a function which you call whenever you want to request query predictions. Takes one argument.1import Autocomplete from "react-google-autocomplete"; 2 3<Autocomplete 4 apiKey={YOUR_GOOGLE_MAPS_API_KEY} 5 style={{ width: "90%" }} 6 onPlaceSelected={(place) => { 7 console.log(place); 8 }} 9 options={{ 10 types: ["(regions)"], 11 componentRestrictions: { country: "ru" }, 12 }} 13 defaultValue="Amsterdam" 14/>;
or
1import { usePlacesWidget } from "react-google-autocomplete"; 2 3export default () => { 4 const { ref } = usePlacesWidget({ 5 apiKey: YOUR_GOOGLE_MAPS_API_KEY, 6 onPlaceSelected: (place) => { 7 console.log(place); 8 }, 9 options: { 10 types: ["(regions)"], 11 componentRestrictions: { country: "ru" }, 12 }, 13 }); 14 15 return <input ref={ref} style={{ width: "90%" }} defaultValue="Amsterdam" />; 16};
1<Autocomplete 2 onPlaceSelected={(place, inputRef, autocomplete) => { 3 console.log(autocomplete); 4 }} 5/>
or
1const { ref, autocompleteRef } = usePlacesWidget({ 2 apiKey: YOUR_GOOGLE_MAPS_API_KEY, 3 onPlaceSelected: (place) => { 4 console.log(place); 5 }, 6});
Formik example lives here
Debounce example lives here
We are planning on rewriting the library with TS/Flow in the later releases but you can already use it with TypeScript because we use declaration files.
If you would like to see something in this library please create an issue and I will implement it as soon as possible.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
SAST tool detected but not run on all commits
Details
Reason
security policy file detected
Details
Reason
Found 11/28 approved changesets -- score normalized to 3
Reason
1 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 1
Reason
9 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn Morereact-google-places-autocomplete
Google places autocomplete input for ReactJS.
react-native-google-places-autocomplete
Customizable Google Places autocomplete component for iOS and Android React-Native apps
react-places-autocomplete
A React component for Google Maps Places Autocomplete
use-places-autocomplete
React hook for Google Maps Places Autocomplete.