Skip to content

Commit da7a7b3

Browse files
committed
Doc: update due to the required callback function of the Maps JS library
1 parent 01eb40b commit da7a7b3

File tree

1 file changed

+31
-41
lines changed

1 file changed

+31
-41
lines changed

README.md

+31-41
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,16 @@ To use this hook, there're two things we need to do:
6767

6868
### Load the library
6969

70-
Use the `script` tag to load the library in your project.
70+
Use the `script` tag to load the library in your project and pass the value of the `callback` parameter to the [callbackName](#parameter-optional) option.
7171

7272
```js
73-
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script>
73+
<script
74+
defer
75+
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=YOUR_CALLBACK_NAME"
76+
></script>
7477
```
7578

76-
We also support asynchronous script loading. By doing so you need to pass the `initMap` to the [callbackName](#parameter-optional) option.
77-
78-
<!-- prettier-ignore-start -->
79-
```js
80-
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initMap"></script>
81-
```
82-
<!-- prettier-ignore-end -->
83-
84-
> ⚠️ If you got a global function not found error. Make sure `usePlaceAutocomplete` is declared before the script was loaded.
79+
> ⚠️ 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](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attributes) attribute of the `<script>` element to achieve that.
8580
8681
### Create the component
8782

@@ -102,6 +97,7 @@ const PlacesAutocomplete = () => {
10297
setValue,
10398
clearSuggestions,
10499
} = usePlacesAutocomplete({
100+
callbackName: "YOUR_CALLBACK_NAME",
105101
requestOptions: {
106102
/* Define search scope here */
107103
},
@@ -186,7 +182,7 @@ const PlacesAutocomplete = () => {
186182
value,
187183
suggestions: { status, data },
188184
setValue,
189-
} = usePlacesAutocomplete();
185+
} = usePlacesAutocomplete({ callbackName: "YOUR_CALLBACK_NAME" });
190186

191187
const handleInput = (e) => {
192188
setValue(e.target.value);
@@ -298,7 +294,7 @@ When use `usePlacesAutocomplete` you can configure the following options via the
298294
| ---------------- | --------------- | -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
299295
| `requestOptions` | object | | The [request options](https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#AutocompletionRequest) of Google Maps Places API except for `input` (e.g. bounds, radius etc.). |
300296
| `googleMaps` | object | `window.google.maps` | In case you want to provide your own Google Maps object, pass the `google.maps` to it. |
301-
| `callbackName` | string | | You can provide a callback name to initialize `usePlacesAutocomplete` after Google script is loaded. It's useful when you [load the script asynchronously](#load-the-library). |
297+
| `callbackName` | string | | The value of the `callback` parameter when [loading the Google Maps JavaScript library](#load-the-library). |
302298
| `debounce` | number | `200` | Number of milliseconds to delay before making a request to Google Maps Places API. |
303299
| `cache` | number \| false | `86400` (24 hours) | Number of seconds to [cache the response data of Google Maps Places API](#cache-data-for-you). |
304300
| `cacheKey` | string | `"upa"` | Optional cache key so one can use multiple caches if needed. |
@@ -360,30 +356,28 @@ const PlacesAutocomplete = () => {
360356
const {
361357
value,
362358
suggestions: { status, data },
363-
setValue
359+
setValue,
364360
} = usePlacesAutocomplete();
365361

366-
const handleSelect = ({ description }) => () => {
367-
// When user select a place, we can replace the keyword without request data from API
368-
// by setting the second parameter to "false"
369-
setValue(description, false);
370-
};
362+
const handleSelect =
363+
({ description }) =>
364+
() => {
365+
// When user select a place, we can replace the keyword without request data from API
366+
// by setting the second parameter to "false"
367+
setValue(description, false);
368+
};
371369

372370
const renderSuggestions = () =>
373-
data.map(suggestion => (
374-
<li
375-
key={suggestion.place_id}
376-
onClick={handleSelect(suggestion)}
377-
>
378-
{/* Render suggestion text */}
379-
</li>
380-
)
381-
});
371+
data.map((suggestion) => (
372+
<li key={suggestion.place_id} onClick={handleSelect(suggestion)}>
373+
{/* Render suggestion text */}
374+
</li>
375+
));
382376

383377
return (
384378
<div>
385379
<input value={value} onChange={handleInput} />
386-
{status === 'OK' && <ul>{renderSuggestions()}</ul>}
380+
{status === "OK" && <ul>{renderSuggestions()}</ul>}
387381
</div>
388382
);
389383
};
@@ -402,29 +396,25 @@ const PlacesAutocomplete = () => {
402396
value,
403397
suggestions: { status, data },
404398
setValue,
405-
clearSuggestions
399+
clearSuggestions,
406400
} = usePlacesAutocomplete();
407401
const ref = useOnclickOutside(() => {
408402
// When user clicks outside of the component, call it to clear and reset the suggestions data
409-
clearSuggestions()
403+
clearSuggestions();
410404
});
411405

412406
const renderSuggestions = () =>
413-
data.map(suggestion => (
414-
<li
415-
key={suggestion.place_id}
416-
onClick={handleSelect(suggestion)}
417-
>
418-
{/* Render suggestion text */}
419-
</li>
420-
)
421-
});
407+
data.map((suggestion) => (
408+
<li key={suggestion.place_id} onClick={handleSelect(suggestion)}>
409+
{/* Render suggestion text */}
410+
</li>
411+
));
422412

423413
return (
424414
<div ref={ref}>
425415
<input value={value} onChange={handleInput} />
426416
{/* After calling the clearSuggestions(), the "status" is reset so the dropdown is hidden */}
427-
{status === 'OK' && <ul>{renderSuggestions()}</ul>}
417+
{status === "OK" && <ul>{renderSuggestions()}</ul>}
428418
</div>
429419
);
430420
};

0 commit comments

Comments
 (0)