Skip to content
This repository was archived by the owner on Aug 29, 2020. It is now read-only.

Commit 502fc0b

Browse files
committed
Prevent API call when input length is 0
1 parent 8c30122 commit 502fc0b

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

lib/use-google-autocomplete.js

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/use-google-autocomplete.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "use-google-autocomplete",
3-
"version": "0.0.8",
3+
"version": "0.0.9",
44
"description": "A tiny React Hook that returns Google Autocomplete results, with session_token handling.",
55
"keywords": [
66
"google maps",

src/use-google-autocomplete.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ export default function useGoogleAutocomplete({
7272
// Cancel previous debounced call.
7373
if (debouncedFn.current) debouncedFn.current.clear()
7474

75+
// If search length is 0, skip sending an API call.
76+
if (query.length === 0) {
77+
dispatch({
78+
type: 'INVALID_REQUEST',
79+
})
80+
return
81+
}
82+
7583
if (!state.isLoading && !abortController.current.signal.aborted) {
7684
dispatch({
7785
type: 'LOADING',
@@ -226,7 +234,7 @@ const reducer = (
226234
predictions: [],
227235
},
228236
isLoading: false,
229-
error: null,
237+
error: `No results — try another input.`,
230238
}
231239
case 'INVALID_REQUEST':
232240
return {
@@ -238,7 +246,7 @@ const reducer = (
238246
return {
239247
...state,
240248
isLoading: false,
241-
error: `Invalid 'key' parameter`,
249+
error: `Invalid 'key' parameter.`,
242250
}
243251
case 'UNKNOWN_ERROR':
244252
return {

0 commit comments

Comments
 (0)