@@ -34,44 +34,46 @@ const searchClient = algoliasearch(
3434function Autocomplete () {
3535 // (1) Create a React state.
3636 const [autocompleteState , setAutocompleteState ] = React .useState ({});
37- const autocomplete = React .useRef (
38- createAutocomplete ({
39- onStateChange ({ state }) {
40- // (2) Synchronize the Autocomplete state with the React state.
41- setAutocompleteState (state);
42- },
43- getSources () {
44- return [
45- // (3) Use an Algolia index source.
46- {
47- sourceId: ' querySuggestionsSource' ,
48- getItemInputValue ({ item }) {
49- return item .query ;
50- },
51- getItems ({ query }) {
52- return getAlgoliaHits ({
53- searchClient,
54- queries: [
55- {
56- indexName: ' instant_search' ,
57- query,
58- params: {
59- hitsPerPage: 4 ,
60- highlightPreTag: ' <mark>' ,
61- highlightPostTag: ' </mark>' ,
37+ const autocomplete = React .useMemo (
38+ () =>
39+ createAutocomplete ({
40+ onStateChange ({ state }) {
41+ // (2) Synchronize the Autocomplete state with the React state.
42+ setAutocompleteState (state);
43+ },
44+ getSources () {
45+ return [
46+ // (3) Use an Algolia index source.
47+ {
48+ sourceId: ' products' ,
49+ getItemInputValue ({ item }) {
50+ return item .query ;
51+ },
52+ getItems ({ query }) {
53+ return getAlgoliaHits ({
54+ searchClient,
55+ queries: [
56+ {
57+ indexName: ' instant_search' ,
58+ query,
59+ params: {
60+ hitsPerPage: 4 ,
61+ highlightPreTag: ' <mark>' ,
62+ highlightPostTag: ' </mark>' ,
63+ },
6264 },
63- } ,
64- ],
65- });
66- },
67- getItemUrl ({ item }) {
68- return item . url ;
65+ ] ,
66+ });
67+ },
68+ getItemUrl ({ item }) {
69+ return item . url ;
70+ },
6971 },
70- },
71- ];
72- },
73- })
74- ). current ;
72+ ];
73+ },
74+ }) ,
75+ []
76+ );
7577
7678 // ...
7779}
0 commit comments