Skip to content

Commit a641c08

Browse files
committed
fix: autocomplete: use state for params, options to work with useMemo
if the objects are passed to useMemo as dependencies directly the internal equality check fails, which defeats the purpose of useMemo as it recalculates the client on every render. That then also generates a new search & debounced search function, which breaks the debouncing.
1 parent 63880b9 commit a641c08

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/autocomplete/autocomplete.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,18 @@ export default ({
2828
const [isLoading, setIsLoading] = useState(false)
2929
const inputRef = useRef()
3030

31+
// setting params & options as state so they can be passed to useMemo as dependencies,
32+
// which doesn’t work if they’re just objects as the internal comparison fails
33+
const [apiParams, setApiParams] = useState(params)
34+
const [apiOptions, setApiOptions] = useState(options)
35+
3136
// Geocode Earth Autocomplete Client
3237
const autocomplete = useMemo(() => {
3338
return createAutocomplete(apiKey, params, {
3439
...options,
3540
client: `ge-autocomplete${typeof VERSION !== 'undefined' ? `-${VERSION}` : ''}`
3641
})
37-
}, [apiKey, params, options])
42+
}, [apiKey, apiParams, apiOptions])
3843

3944
// search queries the autocomplete API
4045
const search = useCallback(text => {

0 commit comments

Comments
 (0)