Skip to content

Commit 6982201

Browse files
committed
docs(examples): updated autosuggest options
1 parent 64e331a commit 6982201

File tree

1 file changed

+41
-14
lines changed
  • examples/react-native/autosuggest

1 file changed

+41
-14
lines changed

examples/react-native/autosuggest/App.tsx

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import {
77
TextInput,
88
View,
99
} from 'react-native';
10-
import { AutosuggestClient, AutosuggestSuggestion } from '@what3words/api';
10+
import {
11+
AutosuggestClient,
12+
AutosuggestInputType,
13+
type AutosuggestOptions,
14+
type AutosuggestSuggestion,
15+
} from '@what3words/api';
1116
import { useState } from 'react';
1217

1318
const API_KEY = '<YOUR_API_KEY>';
@@ -43,19 +48,41 @@ export default function App() {
4348
// Cancel previous request before sending a new one
4449
if (timeoutId) clearTimeout(timeoutId);
4550

46-
timeoutId = setTimeout(async () => {
47-
try {
48-
setIsLoading(true);
49-
setError(null);
50-
const options = { input: search };
51-
const { suggestions } = await client.run(options);
52-
setSuggestions(suggestions);
53-
} catch (error) {
54-
setError(error);
55-
} finally {
56-
setIsLoading(false);
57-
}
58-
}, DEBOUNCE_TIME);
51+
// Debounce the request to avoid sending too many requests while the user is typing
52+
timeoutId = await new Promise(resolve =>
53+
setTimeout(resolve, DEBOUNCE_TIME)
54+
);
55+
56+
try {
57+
setIsLoading(true);
58+
setError(null);
59+
const options: AutosuggestOptions = {
60+
input: search,
61+
inputType: AutosuggestInputType.Text,
62+
nResults: 3,
63+
focus: { lat: 51.521251, lng: -0.203586 },
64+
clipToCountry: ['GB'],
65+
clipToCircle: { center: { lat: 51.521, lng: -0.343 }, radius: 142 },
66+
clipToBoundingBox: {
67+
southwest: { lat: 51.521, lng: -0.343 },
68+
northeast: { lat: 52.6, lng: 2.3324 },
69+
},
70+
clipToPolygon: [
71+
{ lat: 51.521, lng: -0.343 },
72+
{ lat: 52.6, lng: 2.3324 },
73+
{ lat: 54.234, lng: 8.343 },
74+
{ lat: 51.521, lng: -0.343 },
75+
],
76+
language: 'en',
77+
preferLand: true,
78+
};
79+
const { suggestions } = await client.run(options);
80+
setSuggestions(suggestions);
81+
} catch (error) {
82+
setError(error);
83+
} finally {
84+
setIsLoading(false);
85+
}
5986
}
6087

6188
/**

0 commit comments

Comments
 (0)