Skip to content

Commit 1e6ccc0

Browse files
authored
fix: Adding debounce for autocomplete-suggestions example (vercel#2017)
fix: add debounce for autocomplete change handler
1 parent d676bb9 commit 1e6ccc0

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

examples/autocomplete-suggestions/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"license": "MIT",
66
"dependencies": {
77
"@reach/combobox": "0.16.1",
8+
"lodash.debounce": "4.0.8",
89
"next": "latest",
910
"react": "18.1.0",
1011
"react-dom": "18.1.0",

examples/autocomplete-suggestions/pages/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from "react"
1+
import { useMemo, useState } from "react"
22
import fetcher from '../libs/fetcher'
33
import {
44
Combobox,
@@ -7,6 +7,7 @@ import {
77
ComboboxList,
88
ComboboxOption
99
} from '@reach/combobox'
10+
import debounce from 'lodash.debounce'
1011

1112
import useSWR from 'swr'
1213

@@ -17,17 +18,21 @@ export default function Index() {
1718
fetcher
1819
)
1920

20-
function handleSearchTermChange(event) {
21+
function handleChange(event) {
2122
setSearchTerm(event.target.value)
2223
}
2324

25+
const debouncedHandleChange = useMemo(
26+
() => debounce(handleChange, 500)
27+
, []);
28+
2429
return (
2530
<div style={{ textAlign: 'center' }}>
2631
<h1>Country Search</h1>
2732
<Combobox>
2833
<ComboboxInput
2934
className="country-search-input"
30-
onChange={handleSearchTermChange}
35+
onChange={debouncedHandleChange}
3136
aria-label="Countries"
3237
/>
3338
{countries && countries.length > 0 && (

0 commit comments

Comments
 (0)